英文:
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);
}
}
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("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);
}
}
I am using IntelliJ IDEA 2019 Community edition IDE [Free edition].
You can click on buttons highlighted in red color to run the program.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论