【问题标题】:Is there another way to use this boolean in java 11?有没有另一种方法可以在 java 11 中使用这个布尔值?
【发布时间】:2020-03-29 22:23:30
【问题描述】:

所以,我已经学习了两天 java,在编译下面的代码时遇到了错误消息。我试图做的是一个简单的程序,它从系统输入中获取一个名称,然后想知道这是否是您想要使用的名称。工作完美。所以我想修改它:如果这不是所需的名称,您应该能够根据需要随时重新输入名称。这就是为什么我在那里有布尔“确认”,以及while循环。编译时,即使我明确声明并使用它,我也会收到错误消息“未使用已确认的局部变量的值”。我试过简单地移动初始声明,它没有改变任何东西。有谁知道如何解决这个问题或重新做我的循环,这样就不会有任何问题了吗?

仅供参考,我正在使用带有 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();
    }
}

}

【问题讨论】:

  • 嘿!您确实使用 == 运算符比较值。你有confirmed = false
  • @akuzminykh 有帮助。有点尴尬,但这些小事很容易监督......

标签: java variables boolean java-11


【解决方案1】:

使用单个等号是一个赋值,如果你想检查某个东西是否相等,那么使用两个等号

// 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)

【讨论】:

  • 已经修复。非常感谢!这些小语法有时很容易被忽视。
【解决方案2】:

您不应该为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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-09
    • 2020-03-02
    • 2021-07-16
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    相关资源
    最近更新 更多