User Input not working with keyboard.nextLine() and String (Java)

后端 未结 7 591
耶瑟儿~
耶瑟儿~ 2020-12-22 03:06

I recently started learning java during my spare time. So to practice, I\'m making a program that takes a temperature (Celsius or Fahrenheit) and converts it to the opposite

7条回答
  •  礼貌的吻别
    2020-12-22 03:46

    Use Scanner Class:

    int temp;
    java.util.Scanner s = new java.util.Scanner(System.in);
    String opposite, type;
    double product;
    
    System.out.print("Please enter a temperature: ");
    temp = s.nextInt();
    
    System.out.println("Was that in Celsius or Fahrenheit?");
    System.out.print("(Enter 'C' for Celsius and 'F' for Fahrenheit) ");
    type = s.nextLine();
    
    if (type.equals("C")){
    do something
    }
    else{
    do something
    }
    

    For More Input references:

    Scanner

    BufferedReader

    String

    Here is a wonderful comparison on how to compare strings in java.

提交回复
热议问题