gpt4 book ai didi

Java又玩了? (是/否)

转载 作者:行者123 更新时间:2023-12-01 10:24:53 27 4
gpt4 key购买 nike

import java.util.Scanner;
public class PlayAgain {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);
boolean playing = true;
char replayCheck;

do { //start do-while
System.out.print("Play again? (y/n): ");
boolean validInput = false;
while (validInput = false){ //start while
replayCheck = input.next().charAt(0);
switch (replayCheck) { //start switch
case 'y':
case 'Y':
validInput = true;
playing = true;
break;
case 'n':
case 'N':
validInput = true;
playing = false;
break;
default:
System.out.println("Invalid input! please enter (y/n)");
validInput = false;
break;
} //end switch
} //end while
} while (playing = true); //end do-while
System.out.println("Thanks for playing!");
} //end main
} //end class

如果用户输入 n/N程序再次播放,任何其他输入也是如此。逻辑看起来很好,但我在 replayCheck = input.next().charAt(0); 行上得到“分配的值从未被使用”所以我怀疑问题就在那里。

我是个小菜鸟。欢迎任何建议!

最佳答案

将“=”更改为“==”进行比较,您的代码可以正常工作:

import java.util.Scanner;
public class PlayAgain {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);
boolean playing = true;
char replayCheck;

do { //start do-while
System.out.print("Play again? (y/n): ");
boolean validInput = false;
while (validInput == false){ //start while
replayCheck = input.next().charAt(0);
switch (replayCheck) { //start switch
case 'y':
case 'Y':
validInput = true;
playing = true;
break;
case 'n':
case 'N':
validInput = true;
playing = false;
break;
default:
System.out.println("Invalid input! please enter (y/n)");
validInput = false;
break;
} //end switch
} //end while
} while (playing == true); //end do-while
System.out.println("Thanks for playing!");
} //end main
} //end class

关于Java又玩了? (是/否),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35397533/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com