在 java 11 中还有其他方法可以使用此布尔值吗?
Is there another way to use this boolean in java 11?
所以,我已经学习 java 两天了,在编译下面这段代码时遇到错误消息。我试图做的是一个简单的程序,它从系统输入中获取一个名称,然后想知道这是否是您想要使用的名称。工作完美。所以我想修改它:如果这不是所需的名称,您应该可以根据需要随时重新输入名称。这就是为什么我在那里有布尔值 "confirmed" 以及 while 循环。编译时,我收到 "confirmed" 布尔值的错误消息 "The value of the local variable confirmed is not used",即使我明确声明并使用了它。我试过简单地移动初始声明,但它没有改变任何东西。有谁知道如何解决这个问题或重新做我的循环,这样就不会有任何问题了吗?
仅供参考,我正在使用带有 Java 扩展包的 VS Code。
import java.util.*;
public class Name{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
try{
boolean confirmed = false;
while(confirmed = false){
System.out.println("What's your name again?");
String enteredName = sc.nextLine();
System.out.println("So, your name is " + enteredName + "?\nEnter 'yes' to confirm or 'no' to type a new name.");
String confirmation = sc.nextLine();
if(confirmation.equalsIgnoreCase("yes") || confirmation.equalsIgnoreCase("yes.")){
System.out.println("Confirmed, " + enteredName + ".\n\nNow launching.");
confirmed = true;
}
else if(confirmation.equalsIgnoreCase("no") || confirmation.equalsIgnoreCase("no.")){
System.out.println("Please enter a new name.");
confirmed = false;
}
}
}
finally{
sc.close();
}
}
}
使用一个等号是一个赋值,如果你想检查是否相等则使用两个等号
// Sets value of confirmed to false then returns the new value
if (confirmed = false)
// Checks if confirmed is currently equal to false
if (confirmed == false)
// Checks if not confirmed (preferred syntax)
if (!confirmed)
您不应为 System.in
关闭 Scanner
,因为它也会关闭 System.in
。除此之外,您可以按如下方式简化代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String confirmation, enteredName;
do {
System.out.print("What's your name again?");
enteredName = sc.nextLine();
System.out
.print("So, your name is " + enteredName + "?\nEnter 'yes' to confirm or 'no' to type a new name.");
confirmation = sc.nextLine();
if (confirmation.equalsIgnoreCase("yes") || confirmation.equalsIgnoreCase("yes.")) {
System.out.println("Confirmed, " + enteredName + ".\n\nNow launching.");
}
} while (confirmation.equalsIgnoreCase("no") || confirmation.equalsIgnoreCase("no."));
}
}
样本运行:
What's your name again?Arvind
So, your name is Arvind?
Enter 'yes' to confirm or 'no' to type a new name.no
What's your name again?Kumar
So, your name is Kumar?
Enter 'yes' to confirm or 'no' to type a new name.no.
What's your name again?Avinash
So, your name is Avinash?
Enter 'yes' to confirm or 'no' to type a new name.yes
Confirmed, Avinash.
Now launching.
所以,我已经学习 java 两天了,在编译下面这段代码时遇到错误消息。我试图做的是一个简单的程序,它从系统输入中获取一个名称,然后想知道这是否是您想要使用的名称。工作完美。所以我想修改它:如果这不是所需的名称,您应该可以根据需要随时重新输入名称。这就是为什么我在那里有布尔值 "confirmed" 以及 while 循环。编译时,我收到 "confirmed" 布尔值的错误消息 "The value of the local variable confirmed is not used",即使我明确声明并使用了它。我试过简单地移动初始声明,但它没有改变任何东西。有谁知道如何解决这个问题或重新做我的循环,这样就不会有任何问题了吗?
仅供参考,我正在使用带有 Java 扩展包的 VS Code。
import java.util.*;
public class Name{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
try{
boolean confirmed = false;
while(confirmed = false){
System.out.println("What's your name again?");
String enteredName = sc.nextLine();
System.out.println("So, your name is " + enteredName + "?\nEnter 'yes' to confirm or 'no' to type a new name.");
String confirmation = sc.nextLine();
if(confirmation.equalsIgnoreCase("yes") || confirmation.equalsIgnoreCase("yes.")){
System.out.println("Confirmed, " + enteredName + ".\n\nNow launching.");
confirmed = true;
}
else if(confirmation.equalsIgnoreCase("no") || confirmation.equalsIgnoreCase("no.")){
System.out.println("Please enter a new name.");
confirmed = false;
}
}
}
finally{
sc.close();
}
}
}
使用一个等号是一个赋值,如果你想检查是否相等则使用两个等号
// Sets value of confirmed to false then returns the new value
if (confirmed = false)
// Checks if confirmed is currently equal to false
if (confirmed == false)
// Checks if not confirmed (preferred syntax)
if (!confirmed)
您不应为 System.in
关闭 Scanner
,因为它也会关闭 System.in
。除此之外,您可以按如下方式简化代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String confirmation, enteredName;
do {
System.out.print("What's your name again?");
enteredName = sc.nextLine();
System.out
.print("So, your name is " + enteredName + "?\nEnter 'yes' to confirm or 'no' to type a new name.");
confirmation = sc.nextLine();
if (confirmation.equalsIgnoreCase("yes") || confirmation.equalsIgnoreCase("yes.")) {
System.out.println("Confirmed, " + enteredName + ".\n\nNow launching.");
}
} while (confirmation.equalsIgnoreCase("no") || confirmation.equalsIgnoreCase("no."));
}
}
样本运行:
What's your name again?Arvind
So, your name is Arvind?
Enter 'yes' to confirm or 'no' to type a new name.no
What's your name again?Kumar
So, your name is Kumar?
Enter 'yes' to confirm or 'no' to type a new name.no.
What's your name again?Avinash
So, your name is Avinash?
Enter 'yes' to confirm or 'no' to type a new name.yes
Confirmed, Avinash.
Now launching.