确认输入的问题

时间:2019-03-17 07:10:42

标签: java java.util.scanner

基本上,我想确认您输入的名称是正确的,我不太确定问题是java的新问题,并且只是最近才开始花点时间学习代码,任何建议表示感谢,并对不起。这是我的新代码。

import java.util.Scanner;

导入java.util.concurrent.TimeUnit;

公共类用户名{

@SuppressWarnings({ "unused", "resource" })
public static void main(String[] args) throws InterruptedException {


    //This is where you are told to enter your name

    @SuppressWarnings({"resource", "unused"})
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter your name: ");



    @SuppressWarnings("resource")

    // This is where you enter your name
    Scanner scanner = new Scanner(System.in);
    // Your name is then saved as a string so it can be called with "name"
    String name = scanner.nextLine();


    System.out.println("Hello " + name + " lets begin.");



    // This is confirming that your name is correct
    {

        TimeUnit.SECONDS.sleep(2);
    System.out.println("For a test your name is " + name + " correct?");

    // This is the input for confirming your name
    Scanner confirm = new Scanner(System.in);
    String confirmation = scanner.nextLine();
            boolean istrue = true;

    // Meant to print out a line if its = to Yes not working atm
    if(istrue = true){

        System.out.println("Good lets continue");

    }


    }

}

}

1 个答案:

答案 0 :(得分:0)

尝试此代码。

 Scanner confirm = new Scanner(System.in);
    String confirmation = scanner.nextLine();

    // Meant to print out a line if its = to Yes not working atm
    if(confirmation.equals("yes")){

        System.out.println("Good lets continue");

    }