Java if和else语句
我正在练习if和else语句,我猜了一下你的密码字符串,如果你输入了正确的密码,就会出现一条祝贺消息,但是如果你输入了错误的密码,就会出现另一条消息,上面写着“请再试一次”我遇到的问题是,如果用户在重试时猜到了正确的密码,我不知道如何设置另一条祝贺消息Java if和else语句,java,Java,我正在练习if和else语句,我猜了一下你的密码字符串,如果你输入了正确的密码,就会出现一条祝贺消息,但是如果你输入了错误的密码,就会出现另一条消息,上面写着“请再试一次”我遇到的问题是,如果用户在重试时猜到了正确的密码,我不知道如何设置另一条祝贺消息 public static void main(String[] args) { String password = "Shippuden345"; System.out.println("Ente
public static void main(String[] args) {
String password = "Shippuden345";
System.out.println("Enter or guess the password: ");
Scanner scanner = new Scanner(System.in);
String guess = scanner.nextLine();
System.out.println(password.equals(guess));
if (password.equals(guess)) {
System.out.println("Your guess was correct");
return;
}
else;
{
System.out.println("Please try again: ");
Scanner scanner1 = new Scanner(System.in);
String again = scanner1.nextLine();
}
}
}
我想在这里设置另一条消息,如果用户在重试后正确猜出密码
else;
{
System.out.println("Please try again: ");
Scanner scanner1 = new Scanner(System.in);
String again = scanner1.nextLine();
}
}
}最好使用do while循环:
publicstaticvoidmain(字符串[]args){
字符串密码=“Shippuden345”;
字符串猜测;
做{
System.out.println(“输入或猜测密码:”);
guess=scanner.nextLine();
System.out.println(password.equals(guess));
if(密码等于(猜测)){
System.out.println(“您的猜测是正确的”);
返回;
}
否则{
System.out.println(“请重试:”;
}
}而(!password.equals(guess));
}
最好使用do while循环:
publicstaticvoidmain(字符串[]args){
字符串密码=“Shippuden345”;
字符串猜测;
做{
System.out.println(“输入或猜测密码:”);
guess=scanner.nextLine();
System.out.println(password.equals(guess));
if(密码等于(猜测)){
System.out.println(“您的猜测是正确的”);
返回;
}
否则{
System.out.println(“请重试:”;
}
}而(!password.equals(guess));
}
您可以将输入和输入值的处理放在一个无限循环中(例如,while(true){}
),您可以在正确的输入上中断该循环
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String password = "Shippuden345";
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter or guess the password: ");
String guess = scanner.nextLine();
if (password.equals(guess)) {
System.out.println("Your guess was correct");
break;
} else {
System.out.println("Please try again: ");
}
}
}
}
运行示例:
Enter or guess the password: aSD
Please try again:
Enter or guess the password: 12H
Please try again:
Enter or guess the password: Shippuden345
Your guess was correct
您可以将输入值的输入和处理放入无限循环(例如,
while(true){}
)中,您可以在正确的输入上中断该循环
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String password = "Shippuden345";
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter or guess the password: ");
String guess = scanner.nextLine();
if (password.equals(guess)) {
System.out.println("Your guess was correct");
break;
} else {
System.out.println("Please try again: ");
}
}
}
}
运行示例:
Enter or guess the password: aSD
Please try again:
Enter or guess the password: 12H
Please try again:
Enter or guess the password: Shippuden345
Your guess was correct
如果猜测不正确,您可以使用在第一次猜测密码后开始的循环,如果在以下试用中输入的密码不正确,则可以继续循环
然后在密码匹配时中断循环并打印祝贺消息。如果猜测不正确,您可以使用在第一次猜测密码后开始的循环,如果在以下测试中输入的密码不正确,则继续循环
然后在密码匹配时中断循环并打印祝贺消息。我已编辑了您的代码。但是,我已将其添加到静态空白中: 我还添加了一个
int
,以便在第一次尝试不正确时显示。希望你喜欢
class s{
public static void check(){
int attempts = 0;
String password = "Shippuden345";
System.out.println("Enter or guess the password: ");
Scanner scanner = new Scanner(System.in);
String guess = scanner.nextLine();
System.out.println(password.equals(guess));
while(attempts < 5){
if (password.equals(guess) && attempts == 0) {
System.out.println("Your guess was correct");
return;
}
else
if (password.equals(guess) && attempts > 0) {
System.out.println("Attempt number "+attempts+" was correct.");
return;
}
else{
System.out.println("Please try again: ");
attempts++;
guess = scanner.nextLine();
}
}
}
public static void main(String[] args) {
check();
}
}```
s类{
公共静态无效检查(){
int=0;
字符串密码=“Shippuden345”;
System.out.println(“输入或猜测密码:”);
扫描仪=新的扫描仪(System.in);
字符串猜测=scanner.nextLine();
System.out.println(password.equals(guess));
while(尝试次数<5次){
if(password.equals(guess)&&truments==0){
System.out.println(“您的猜测是正确的”);
返回;
}
其他的
if(密码等于(猜测)&尝试次数>0){
System.out.println(“尝试次数”+尝试次数+“正确”);
返回;
}
否则{
System.out.println(“请重试:”;
尝试++;
guess=scanner.nextLine();
}
}
}
公共静态void main(字符串[]args){
检查();
}
}```
我已经编辑了您的代码。但是,我已将其添加到静态空白中:
我还添加了一个int
,以便在第一次尝试不正确时显示。希望你喜欢
class s{
public static void check(){
int attempts = 0;
String password = "Shippuden345";
System.out.println("Enter or guess the password: ");
Scanner scanner = new Scanner(System.in);
String guess = scanner.nextLine();
System.out.println(password.equals(guess));
while(attempts < 5){
if (password.equals(guess) && attempts == 0) {
System.out.println("Your guess was correct");
return;
}
else
if (password.equals(guess) && attempts > 0) {
System.out.println("Attempt number "+attempts+" was correct.");
return;
}
else{
System.out.println("Please try again: ");
attempts++;
guess = scanner.nextLine();
}
}
}
public static void main(String[] args) {
check();
}
}```
s类{
公共静态无效检查(){
int=0;
字符串密码=“Shippuden345”;
System.out.println(“输入或猜测密码:”);
扫描仪=新的扫描仪(System.in);
字符串猜测=scanner.nextLine();
System.out.println(password.equals(guess));
while(尝试次数<5次){
if(password.equals(guess)&&truments==0){
System.out.println(“您的猜测是正确的”);
返回;
}
其他的
if(密码等于(猜测)&尝试次数>0){
System.out.println(“尝试次数”+尝试次数+“正确”);
返回;
}
否则{
System.out.println(“请重试:”;
尝试++;
guess=scanner.nextLine();
}
}
}
公共静态void main(字符串[]args){
检查();
}
}```
一个简单的方法是将if(password.equals(guess)){
逻辑复制到else
块中,创建一个嵌套的if-else
语句。如果您想允许两次以上的猜测,您应该了解“循环”的概念你的else
后面的分号不属于那里。它起作用是因为你的if
中有一个return
,所以它会在那里停止函数,但它下面的块现在不是else
的一部分。(为了弄清楚为什么上面的建议是重复的:不正确的密码和无效的输入值都需要相同的处理——使用循环——才能无限期地重新编译)。一种简单的方法是复制if(password.equals(guess)){
将逻辑放入else
块,创建一个嵌套的if else
语句。如果您想允许两次以上的猜测,您应该了解“循环”的概念你的else
后面的分号不属于那里。它起作用是因为你的if
中有一个return
,所以它会在那里停止函数,但它下面的块现在不是else
的一部分。(为了弄清楚为什么上面的建议是重复的:不正确的密码和无效的输入值都需要相同类型的处理——使用循环——才能无限期地重新编程)。OP可以使用几种不同类型的循环。您可以尝试while循环,当密码不正确时,输出“请重试”,然后在