“打印”;我们没有';我没有找到这封信;带循环的java
这是我的节目“打印”;我们没有';我没有找到这封信;带循环的java,java,loops,Java,Loops,这是我的节目 import java.util.*; public class LeTter { //open class name static Scanner keyboard = new Scanner(System.in); // Scanner public static void main(String[] args) { //open main class char letter; //decl int y, x, z; //decl
import java.util.*;
public class LeTter { //open class name
static Scanner keyboard = new Scanner(System.in); // Scanner
public static void main(String[] args) { //open main class
char letter; //decl
int y, x, z; //decl
boolean check = false;
System.out.println("Enter 8 letters: "); // print sentince
char[] des = new char[8]; //array
for (y = 0; y < des.length; y++) { // for without semicolon :), open for loop
des[y] = keyboard.next().charAt(0); //initialize
} // close for loop
System.out.println("Enter any letter to check about it: "); //will apper to the user
letter = keyboard.next().charAt(0); //initi
for (x = 0; x < des.length; x++) { //for loop opens
if (des[x] == letter) { //open if loop
System.out.println("we found it's " + des[x] + " and the index of it is " + x);
System.out.println("so we will show you this letters " + x + " times");
for (z = 0; z < x; z++)
System.out.println(des[x] + " ");
check = false;
} //close if loop
if (true)
System.out.println("are you sure about the letter's you enterd it? we didnt found it ");
} // close for loop
} //close main
} //close class
import java.util.*;
公共类字母{//打开类名称
静态扫描仪键盘=新扫描仪(System.in);//扫描仪
公共静态void main(字符串[]args){//open main类
字符字母;//decl
int y,x,z;//decl
布尔检查=假;
System.out.println(“输入8个字母:”;//打印感知
char[]des=新字符[8];//数组
对于(y=0;y
如果用户没有输入看起来不像8个字母的字母,我想打印这句话“我们没有找到它”
我该怎么做
示例:
输入8个字母
a s d f g h j k
输入任何字母
T
我们没有找到它我跳过输入,从代码的循环开始,将其更改为:
for (x = 0; x < des.length; x++)
{ // for loop opens
if (des[x] == letter)
{ // open if loop
check = true;
System.out.println("we found it's " + des[x] + " and the index of it is " + x);
break; // you want to show 1st one only, right?
} // close if loop
} // close for loop
if (!check)
System.out.println("are you sure about the letter's you enterd it? we didnt found it ");
for(x=0;x
我缩短了您的代码,因为您只需将用户的输入添加到字符
数组
,然后删除空格即可
我用了break
要在找到第一个char
后退出循环,如果您想知道它的多个实例,可以将其删除
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter 8 letters: ");
String letters = keyboard.nextLine().replaceAll(" ", "");
char cArray[] = letters.toCharArray();
System.out.println("Enter any letter to check about it: ");
char letter = keyboard.next().charAt(0);
boolean found = false;
for (int i = 0; i < cArray.length; i++) {
if (cArray[i] == letter) {
System.out.println("We found " + letter + " at position " + i);
found = true;
break; //used as you specify only to find the first one
}
}
if (!found) {
System.out.println("Are you sure about the letter you entered? We didn't find it");
}
}
静态扫描仪键盘=新扫描仪(System.in);
公共静态void main(字符串[]args){
System.out.println(“输入8个字母:”);
字符串字母=键盘.nextLine().replaceAll(“,”);
char cArray[]=letters.toCharArray();
System.out.println(“输入任何要检查的字母:”);
字符字母=键盘.next().charAt(0);
布尔值=false;
对于(int i=0;i