Java 为什么最后一条else语句没有在这里打印/执行?
最后一个else语句不会打印任何内容。。。 只需接收输入并停止程序。我做错了什么 我是全新的,刚刚开始学习java。以前没有任何代码方面的经验。对不起 试着检查语法在我看来一切正常Java 为什么最后一条else语句没有在这里打印/执行?,java,if-statement,Java,If Statement,最后一个else语句不会打印任何内容。。。 只需接收输入并停止程序。我做错了什么 我是全新的,刚刚开始学习java。以前没有任何代码方面的经验。对不起 试着检查语法在我看来一切正常 import java.util.Scanner; public class MyClass { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int
import java.util.Scanner;
public class MyClass
{
public static void main(String[] args)
{
Scanner scn = new Scanner(System.in);
int x =7;
System.out.println("Enter a Number from 1 to 10: ");
int guess = scn.nextInt();
while(x != guess)
{
if(x < guess) {
System.out.println("Guess Lower...");
guess = scn.nextInt();
}
else if (x > guess) {
System.out.println("Guess Higher...");
guess = scn.nextInt();
}
else {
System.out.println("Correct Guess!");
}
}
}
}
所以如果我输入7
它应该说/显示
猜对了
但它不会显示任何东西
“猜低”和“猜高”可以很好地工作。当比较正确的数字时,代码会在循环时立即转义,所以最后一个else语句无法访问 您可以像下面这样做: 导入java.util.Scanner; 公共类MyClass { 公共静态无效字符串[]args { 扫描仪scn=新的ScannerSystem.in; int x=7; System.out.Printl输入1到10之间的数字:; int guess=scn.nextInt; 猜猜看{ ifx
当比较正确的数字时,代码在循环时立即转义,所以最后一个else语句不可访问 您可以像下面这样做: 导入java.util.Scanner; 公共类MyClass { 公共静态无效字符串[]args { 扫描仪scn=新的ScannerSystem.in; int x=7; System.out.Printl输入1到10之间的数字:; int guess=scn.nextInt; 猜猜看{ ifx
Scanner scn = new Scanner(System.in);
int x = 7;
System.out.println("Enter a Number from 1 to 10: ");
int guess = scn.nextInt();
while (x != guess) {
if (x == guess)
System.out.println("correct");
else if (x < guess) {
System.out.println("Guess Lower...");
guess = scn.nextInt();
}
else if (x > guess) {
System.out.println("Guess Higher...");
guess = scn.nextInt();
}
}
System.out.println("correct");
}
你应该得到这个代码System.out.println正确猜测!退出循环,因为它将循环,直到找到猜测的值,而不转到下一行代码
请参阅下面的代码
Scanner scn = new Scanner(System.in);
int x = 7;
System.out.println("Enter a Number from 1 to 10: ");
int guess = scn.nextInt();
while (x != guess) {
if (x == guess)
System.out.println("correct");
else if (x < guess) {
System.out.println("Guess Lower...");
guess = scn.nextInt();
}
else if (x > guess) {
System.out.println("Guess Higher...");
guess = scn.nextInt();
}
}
System.out.println("correct");
}
您的问题是while循环的条件。 看看Java中的递归。 可以这样做:
import java.util.*;
public class MyClass
{
public static void main(String []args)
{
new_guess();
}
static void new_guess()
{
int correct_answer = 7;
Scanner scn = new Scanner(System.in);
System.out.println("Enter a Number from 1 to 10: ");
int guess = scn.nextInt();
if(correct_answer != guess)
{
if(correct_answer < guess)
{
System.out.println("Guess Lower...");
}
else if (correct_answer > guess)
{
System.out.println("Guess Higher...");
}
new_guess();
}
else
{
System.out.println("Correct Guess!");
}
}
}您的问题是while循环的条件。 看看Java中的递归。 可以这样做:
import java.util.*;
public class MyClass
{
public static void main(String []args)
{
new_guess();
}
static void new_guess()
{
int correct_answer = 7;
Scanner scn = new Scanner(System.in);
System.out.println("Enter a Number from 1 to 10: ");
int guess = scn.nextInt();
if(correct_answer != guess)
{
if(correct_answer < guess)
{
System.out.println("Guess Lower...");
}
else if (correct_answer > guess)
{
System.out.println("Guess Higher...");
}
new_guess();
}
else
{
System.out.println("Correct Guess!");
}
}
}如果要确认它的相等性,则不需要在运行时引入指定的块,直到它是一个false。当表达式返回false时,它将自动打印出下一步内容
while(x != guess)
{
if(x < guess) {
System.out.println("Guess Lower...");
guess = scn.nextInt();
}
else if (x > guess) {
System.out.println("Guess Higher...");
guess = scn.nextInt();
}
}
System.out.println("correct!");
}
本质上,您的代码应该以这种方式进行改革这是一个while循环,它会一直运行,直到指定的表达式为false,因此您不需要引入else块来确认guess变量的相等性。当表达式返回false时,它将自动打印出下一步内容
while(x != guess)
{
if(x < guess) {
System.out.println("Guess Lower...");
guess = scn.nextInt();
}
else if (x > guess) {
System.out.println("Guess Higher...");
guess = scn.nextInt();
}
}
System.out.println("correct!");
}
基本上,您的代码应该以这种方式进行改革这是您的while条件。它在else块有机会运行之前就脱离了循环。但最后一个else包含在其中,而不是吗?@S.S.不,最后一个else包含在if中,不是whileoh ok,那么x变成=guess,它混淆了while条件,在这种情况下,while x不是eq to guess?@S.S我想@liya Bursov想说的是,最后一次执行else的唯一时间是当x==guess。但是“您的while循环仅在x时运行!”猜猜看,这是你的情况。它在else块有机会运行之前就脱离了循环。但最后一个else包含在其中,而不是吗?@S.S.不,最后一个else包含在if中,不是whileoh ok,那么x变成=guess,它混淆了while条件,在这种情况下,while x不是eq to guess?@S.S我想@liya Bursov想说的是,最后一次执行else的唯一时间是当x==guess。但是“您的while循环仅在x时运行!”我猜。天哪……我以前从没见过这个。感谢分享,我什么都不懂,但是,这看起来既吓人又有趣。java中的递归是一个方法不断调用自身的过程。在某些条件下调用是非常重要的,比如在您的情况下-当用户猜测不正确时,方法会不断调用自己。。。美好的我不知道你能做这个->新猜;是时候好好学习了……我以前从没见过。感谢分享,我什么都不懂,但是,这看起来既吓人又有趣。java中的递归是一个方法不断调用自身的过程。在某些条件下调用是非常重要的,比如在您的情况下-当用户猜测不正确时,方法会不断调用自己。。。 美好的我不知道你能做这个->新猜;该学习了