Java 尽管满足条件,但循环不循环
这是我到目前为止的代码,while循环:Java 尽管满足条件,但循环不循环,java,loops,while-loop,Java,Loops,While Loop,这是我到目前为止的代码,while循环: public class Lab10d { public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); char response = 0; //add in a do while loop after you get the basics up and running String player =
public class Lab10d
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
char response = 0;
//add in a do while loop after you get the basics up and running
String player = "";
out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
//read in the player value
player = keyboard.next();
RockPaperScissors game = new RockPaperScissors(player);
game.setPlayers(player);
out.println(game);
while(response == ('y'))
{
out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
player = keyboard.next();
game.setPlayers(player);
//game.determineWinner();
out.println(game);
out.println();
//
}
out.println("would you like to play again? (y/n):: ");
String resp = keyboard.next();
response = resp.charAt(0);
}
}
它应该额外运行代码,直到输入n
当我输入y时,它应该重新运行代码,但不会在你询问他们是否想再次播放之前,你的while循环结束
将循环更改为:
while(response == ('y'))
{
out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
player = keyboard.next();
game.setPlayers(player);
//game.determineWinner();
out.println(game);
out.println();
out.println("would you like to play again? (y/n):: ");
String resp = keyboard.next();
response = resp.charAt(0);
}
还有一个问题:在循环启动之前,响应没有设置为“y”。它根本不会在循环中做任何事情。使用do{…}while response=='y'循环代替
do
{
out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
player = keyboard.next();
game.setPlayers(player);
//game.determineWinner();
out.println(game);
out.println();
out.println("would you like to play again? (y/n):: ");
String resp = keyboard.next();
response = resp.charAt(0);
} while (response == 'y');
do while将执行代码一次,然后检查条件,如果条件为真,则继续执行。while循环将只检查条件,并在条件为真时继续执行
编辑:我为您整理了一些代码:
import java.util.Scanner;
public class Troubleshoot {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
char response = ' ';
do {
System.out.println("Stuff");
System.out.print("Again? (y/n): ");
response = s.next().charAt(0);
} while (response == 'y');
}
}
输出:
Stuff
Again? (y/n): y
Stuff
Again? (y/n): y
Stuff
Again? (y/n): n
你的while循环在你询问他们是否想再次玩之前结束
将循环更改为:
while(response == ('y'))
{
out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
player = keyboard.next();
game.setPlayers(player);
//game.determineWinner();
out.println(game);
out.println();
out.println("would you like to play again? (y/n):: ");
String resp = keyboard.next();
response = resp.charAt(0);
}
还有一个问题:在循环启动之前,响应没有设置为“y”。它根本不会在循环中做任何事情。使用do{…}while response=='y'循环代替
do
{
out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
player = keyboard.next();
game.setPlayers(player);
//game.determineWinner();
out.println(game);
out.println();
out.println("would you like to play again? (y/n):: ");
String resp = keyboard.next();
response = resp.charAt(0);
} while (response == 'y');
do while将执行代码一次,然后检查条件,如果条件为真,则继续执行。while循环将只检查条件,并在条件为真时继续执行
编辑:我为您整理了一些代码:
import java.util.Scanner;
public class Troubleshoot {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
char response = ' ';
do {
System.out.println("Stuff");
System.out.print("Again? (y/n): ");
response = s.next().charAt(0);
} while (response == 'y');
}
}
输出:
Stuff
Again? (y/n): y
Stuff
Again? (y/n): y
Stuff
Again? (y/n): n
…你的问题是?你的括号不匹配?我们能看到完整的代码吗?括号被去掉是因为我复制/粘贴了代码的摘录…你的问题是?你的括号不匹配?我们能看到完整的代码吗?括号被去掉了,因为我复制/粘贴了一段代码的摘录,他可能在上面设置了y,这并不太重要,更多的问题是循环在他问是否想再次玩之前结束了。由于out.println之前的括号,查看他的完整代码,您对未设置响应的看法是正确的,同时告诉他在顶部将响应设置为“y”,而不是0-或者只是做一会儿lol@tech_geek23不,do-while保证总是执行一次,但是while循环有时根本无法执行。do while将执行代码一次,然后检查条件,如果条件为真,则继续执行。while循环将只检查条件,并在其为真时继续执行。将其更改为do while并再次运行,但它应该询问您是否希望在首次运行后再次播放。当它在第二次运行后询问时,当我输入一个y时,它没有再次运行。它应该做的是运行一次,然后询问你是否想再次运行,如果输入“y”,它将继续运行。如果没有。。。好吧,你的代码的其余部分出了问题。他可能在上面设置了y,这不太重要,更多的问题是,在他问是否想再次玩之前,循环结束了。由于out.println之前的括号,查看他的完整代码,您对未设置响应的看法是正确的,同时告诉他在顶部将响应设置为“y”,而不是0-或者只是做一会儿lol@tech_geek23不,do-while保证总是执行一次,但是while循环有时根本无法执行。do while将执行代码一次,然后检查条件,如果条件为真,则继续执行。while循环将只检查条件,并在其为真时继续执行。将其更改为do while并再次运行,但它应该询问您是否希望在首次运行后再次播放。当它在第二次运行后询问时,当我输入一个y时,它没有再次运行。它应该做的是运行一次,然后询问你是否想再次运行,如果输入“y”,它将继续运行。如果没有。。。好吧,你剩下的代码有点不对劲。