我该如何使其运行

huangapple go评论65阅读模式
英文:

How do I make this run

问题

package mychoices;

import java.io.IOException;
import java.util.Scanner;

public class MyChoices {

    public static void main(String[] args) throws IOException {

        Scanner in = new Scanner(System.in);
        Scanner ls = new Scanner(System.in);

        System.out.println("What's your name?");
        String next = in.next();
        char userName = (char)System.in.read();

        System.out.println("Do you like rock music?");
        System.out.println("Enter Y for yes or N for no");
        char userChoice = (char) System.in.read();
        String Y = ls.next();

        if(Y.equals('Y')) {
            System.out.println("Recommendations");
        }
        System.out.println("Thanks for visiting, " + (ls));
    }
}
英文:

How do I make this run:

package mychoices;

import java.io.IOException;
import java.util.Scanner;


public class MyChoices {
   
    public static void main(String[] args) throws IOException {

        Scanner in = new Scanner(System.in);    
        Scanner ls = new Scanner(System.in);
        
        System.out.println("What's your name?");
        String next = in.next();
        char userName = (char)System.in.read();
        
        System.out.println("Do you like rock music?");
        System.out.println("Enter Y for yes or N for no");
        char userChoice = (char) System.in.read();
        String Y = ls.next();
            
        if(Y.equals('Y')) {   
            System.out.println("Recomendations");
        }               
        System.out.println("Thanks for visiting, " + (ls));            
    }
}

答案1

得分: 0

首先,不要使用两个都从System.in读取的Scanners。

你不能使用以下代码将String转换为char:
char userName = (char)System.in.read();

删除那行代码,因为看起来你没有在任何地方使用userName

在这里也有相同的错误:char userChoice = (char)System.in.read();
将其替换为char userChoice = in.read().charAt(0);

现在你有了一个字符,将 if(Y.equals('Y')){
替换为if (userChoice == 'Y') {

英文:

Firstly, don't use two Scanners that are both reading from System.in.

You cannot cast a String to a char with:
char userName = (char)System.in.read();

delete that line, since it seems you do not use userName anywhere.

Same mistake here: char userChoice = (char)System.in.read();
Replace that with char userChoice = in.read().charAt(0);

Now that you have a character, replace if(Y.equals('Y')){
with if (userChoice == 'Y') {

答案2

得分: -1

import java.io.IOException;
import java.util.Scanner;

public class MyChoices {

    public static void main(String[] args) throws IOException {

        Scanner in = new Scanner(System.in);
        Scanner ls = new Scanner(System.in);

        System.out.println("What's your name?");
        String userName = in.nextLine(); //Get user name

        System.out.println("Do you like rock music?");
        System.out.println("Enter Y for yes or N for no");
        char userChoice = in.next().charAt(0);

        if(userChoice=='Y'){
            System.out.println("Recommendations");
        }
        System.out.println("Thanks for visiting, " + userName);

    }
}

Refer screen shot

I am using IntelliJ IDEA 2019 Community edition IDE [Free edition].

You can click on buttons highlighted in red color to run the program.


<details>
<summary>英文:</summary>

You can try below code. 

```java
import java.io.IOException;
import java.util.Scanner;

public class MyChoices {

    public static void main(String[] args) throws IOException {

        Scanner in = new Scanner(System.in);
        Scanner ls = new Scanner(System.in);

        System.out.println(&quot;What&#39;s your name?&quot;);
        String userName = in.nextLine(); //Get user name

        System.out.println(&quot;Do you like rock music?&quot;);
        System.out.println(&quot;Enter Y for yes or N for no&quot;);
        char userChoice = in.next().charAt(0);

        if(userChoice==&#39;Y&#39;){
            System.out.println(&quot;Recommendations&quot;);
        }
        System.out.println(&quot;Thanks for visiting, &quot; + userName);

    }
}

Refer screen shot

I am using IntelliJ IDEA 2019 Community edition IDE [Free edition].

You can click on buttons highlighted in red color to run the program.

huangapple
  • 本文由 发表于 2020年9月12日 14:16:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63857441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定