不接受用户输入并跳转到 while 循环的 else 子句

huangapple go评论155阅读模式
英文:

Not taking user input and jumping to else clause in a while loop

问题

我是JAVA的初学者,一直在尝试创建一个简单的计算器程序。我在while循环中遇到了问题,循环不等待我的输入"ans",直接跳到else子句。

import java.util.Scanner;

public class scanner_program {

	public static void main(String[] args)
	{
		
		Scanner ob = new Scanner(System.in);
		int i = 1;
		while (i==1) 
		{	
			System.out.print("Enter a number: ");
			int num = ob.nextInt();
			System.out.print("Enter another number: ");
			int num2 = ob.nextInt();
		
			System.out.println("Sum of "+num+" And "+num2+" is "+(num+num2));
			System.out.println("Remainder of "+num+" And "+num2+" is "+(num-num2));
			System.out.println("Product of "+num+" And "+num2+" is "+(num*num2));
			float answer= (float) num/num2;
			System.out.println("Quotient of "+num+" And "+num2+" is "+answer);
		
			System.out.print("Try Again? (Y/N) ");
			String ans = ob.nextLine();
		
			if (ans.equals("Y") || ans.equals("y")) {
				// do nothing
			}
			else if (ans.equals("N") || ans.equals("n")){
				break;
			}
			else {
				System.out.println("Invalid input");
				break;
				}
			}	
	
		}
}
英文:

Im a beginner in JAVA and I have been trying to create a simple calculator program. I am facing a problem in the while loop where the loop does not wait for my input "ans" and directly jumps to the else clause

import java.util.Scanner;
public class scanner_program {
public static void main(String[] args)
{
Scanner ob = new Scanner(System.in);
int i = 1;
while (i==1) 
{	
System.out.print("Enter a number: ");
int num = ob.nextInt();
System.out.print("Enter another number: ");
int num2 = ob.nextInt();
System.out.println("Sum of "+num+" And "+num2+" is "+(num+num2));
System.out.println("Remainder of "+num+" And "+num2+" is "+(num-num2));
System.out.println("Product of "+num+" And "+num2+" is "+(num*num2));
float answer= (float) num/num2;
System.out.println("Quotient of "+num+" And "+num2+" is "+answer);
System.out.print("Try Again? (Y/N) ");
String ans = ob.nextLine();
if (ans=="Y" || ans=="y" ) {
// do nothing
}
else if (ans=="N" || ans=="n"){
break;
}
else {
System.out.println("Invalid input");
break;
}
}	
}
}

答案1

得分: 1

ob.nextLine()将换行符视为其下一个值,因此ans被设置为无效字符。

ob.nextLine()更改为ob.next()

另外,对于Java,您应该使用string.equals("another string"),所以将您的字母检查更改为以下内容:

if (ans.equals("Y") || ans.equals("y")) {
  // 什么都不做
}
else if (ans.equals("N") || ans.equals("n")){
  break;
}
英文:

ob.nextLine() is taking the newline character as its next value, so ans is set to an invalid character.

change ob.nextLine() to ob.next().

Also, for Java, you should use string.equals("another string") so change your letter checking to this:

if (ans.equals("Y") || ans.equals("y") ) {
// do nothing
}
else if (ans.equals("N") || ans.equals("n")){
break;
}

答案2

得分: 0

public static void main(String[] args){
Scanner ob = new Scanner(System.in);
while(true){
System.out.print("输入一个数字: ");
int num = ob.nextInt();
System.out.print("输入另一个数字: ");
int num2 = ob.nextInt();

    System.out.println(num + " 和 " + num2 + " 的和为 " + (num + num2));
System.out.println(num + " 和 " + num2 + " 的余数为 " + (num - num2));
System.out.println(num + " 和 " + num2 + " 的乘积为 " + (num * num2));
float answer= (float) num / num2;
System.out.println(num + " 和 " + num2 + " 的商为 " + answer);
System.out.print("再试一次? (Y/N)");
ob.nextLine();
String ans = ob.nextLine();
if (ans.equalsIgnoreCase("y")) {
// 什么都不做
} else if (ans.equalsIgnoreCase("n")){
break;
} else {
System.out.println("无效输入");
break;
}
} 

}

英文:
public static void main(String[] args){
Scanner ob = new Scanner(System.in);
while(true){
System.out.print("Enter a number: ");
int num = ob.nextInt();
System.out.print("Enter another number: ");
int num2 = ob.nextInt();
System.out.println("Sum of "+num+" And "+num2+" is "+(num+num2));
System.out.println("Remainder of "+num+" And "+num2+" is "+(num-num2));
System.out.println("Product of "+num+" And "+num2+" is "+(num*num2));
float answer= (float) num/num2;
System.out.println("Quotient of "+num+" And "+num2+" is "+answer);
System.out.print("Try Again? (Y/N)");
ob.nextLine();
String ans = ob.nextLine();
if (ans.equalsIgnoreCase("y")) {
// do nothing
} else if (ans.equalsIgnoreCase("n")){
break;
} else {
System.out.println("Invalid input");
break;
}
} 
}

You should enter ob.nextLine() to clear your input for other inputs you may enter.

huangapple
  • 本文由 发表于 2020年7月22日 01:28:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63019878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定