Java 我很难让我的循环工作
我很难让环路正常工作。如果用户在开始时键入“Quit”或“Quit”,循环应该有一个out,但如果没有输入Quit,则继续处理 我已经尝试了一个do循环,但是因为一旦它不能使用给定的参数,它就会运行循环。我也无法让if/else语句发挥作用。我正在按照教授的指示使用Eclipse进行编码/编译 以下是我当前的代码片段:Java 我很难让我的循环工作,java,loops,Java,Loops,我很难让环路正常工作。如果用户在开始时键入“Quit”或“Quit”,循环应该有一个out,但如果没有输入Quit,则继续处理 我已经尝试了一个do循环,但是因为一旦它不能使用给定的参数,它就会运行循环。我也无法让if/else语句发挥作用。我正在按照教授的指示使用Eclipse进行编码/编译 以下是我当前的代码片段: public static void main(String[] args) { Scanner scan= new Scanner (System.in); S
public static void main(String[] args) {
Scanner scan= new Scanner (System.in);
String fullName;
int total= 0;
System.out.print("Please enter your full name: ");
fullName= scan.nextLine(); //Scans user input for name
int count= fullName.length()-1;//Evaluates the length of the user's name
while (fullName.equalsIgnoreCase("Quit || quit"))
{
System.out.println("Thank you for using the system. Come back soon.");
}
{
System.out.println("Please enter "+count+" numbers and the total will be calculated\n");
for (int i=0; i<count; i++) {
int userEnteredNum= scan.nextInt();
total += userEnteredNum;
}
System.out.println("Good day, "+ fullName+ "! You have entered "+
count+ " numbers.\nThe total of all the numbers is "+ total);
}
while (fullName.equalsIgnoreCase("Quit || quit"));
}
publicstaticvoidmain(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
字符串全名;
int-total=0;
System.out.print(“请输入您的全名:”);
fullName=scan.nextLine();//扫描用户输入的名称
int count=fullName.length()-1;//计算用户名的长度
while(fullName.equalsIgnoreCase(“Quit | | Quit”))
{
System.out.println(“感谢您使用该系统,请尽快回来。”);
}
{
System.out.println(“请输入“+count+”数字并计算总数\n”);
对于(int i=0;i您得到的注释和答案是正确的,条件是fullName.equalsIgnoreCase(“Quit | | Quit”)
如果您编写Quit | | Quit,就直接输入。
如果使用equalsIgnoreCase,则可以使用Quit或Quit,如果要使用| |
,则可以使用fullName.equals(“Quit”)| | fullName.equals(“Quit”)
。逻辑运算符必须在字符串外部进行比较当出现Quit字符串时,您没有退出程序,请尝试此操作
if (fullName.equalsIgnoreCase("quit"))
{
System.out.println("Thank you for using the system. Come back soon.");
System.exit(0);
}
publicstaticvoidmain(字符串[]args){
while(true){
扫描仪扫描=新扫描仪(System.in);
字符串全名;
int-total=0;
System.out.print(“请输入您的全名:”);
fullName=scan.nextLine();//扫描用户输入的名称
int count=fullName.length()-1;//计算用户名的长度
if(fullName.equalsIgnoreCase(“退出”)){
System.out.println(“感谢您使用该系统,请尽快回来。”);
返回;
}
System.out.println(“请输入“+count+”数字并计算总数\n”);
for(int i=0;i
检查“Quit | | Quit”
意味着用户按字面意思输入该字符串,而不是“Quit”
或“Quit”
。如果使用equalsIgnoreCase
,则不必同时测试“Quit”和“Quit”。为什么额外的while
循环在底部?而且,您的循环中从不更新全名
,因此当输入时它们不会终止。我认为您的第一个while
应该是if
,并且在最后一个代码块之前应该有一个else
。
public static void main(String[] args) {
while (true) {
Scanner scan = new Scanner(System.in);
String fullName;
int total = 0;
System.out.print("Please enter your full name: ");
fullName = scan.nextLine(); //Scans user input for name
int count = fullName.length() - 1;//Evaluates the length of the user's name
if (fullName.equalsIgnoreCase("Quit")) {
System.out.println("Thank you for using the system. Come back soon.");
return;
}
System.out.println("Please enter " + count + " numbers and the total will be calculated\n");
for (int i = 0; i < count; i++) {
int userEnteredNum = scan.nextInt();
total += userEnteredNum;
}
System.out.println("Good day, " + fullName + "! You have entered " +
count + " numbers.\nThe total of all the numbers is " + total);
}
}