gpt4 book ai didi

java - RandomInt 和 If Else 问题

转载 作者:行者123 更新时间:2023-11-30 06:59:54 25 4
gpt4 key购买 nike

我是 Java 的新手,我反复尝试修复这个问题......我有三十个错误

* @(#)Input.java
*
* Input application
*
* @author
* @version 1.00 2015/6/24
*/

import javax.swing.JOptionPane;
import java.util.Random;

public class Input {

public static void main(String[] args) {

// TODO, add your application code

String name = JOptionPane.showInputDialog("Rock, Paper, or Scissors?");
System.out.println(name+"!");
Random rn = new Random();
random.nextInt(10 - 1 + 1) + 1;
}


if (name.equalsIgnoreCase("Rock")){
if (0<randomInt && randomInt<4){
System.out.println("Paper! You Lose!");
}
else if(4<= randomInt &&randomInt <=6){
System.out.println("Rock! We Tied!");
}
else{
System.out.println("Scissors! You Win!");
}
}
if (name.equalsIgnoreCase("Paper")){
if (0<randomInt && randomInt<4){
System.out.println("Scissors! You Lose!");
}
else if(4<= randomInt &&randomInt <=6){
System.out.println("Paper! We Tied!");
}
else{
System.out.println("Rock! You Win!");
}
}
}
if (name.equalsIgnoreCase("Scissors")){
if (0<randomInt && randomInt<4){
System.out.println("Rock! You Lose!");
}
else if(4<= randomInt &&randomInt <=6){
System.out.println("Scissors! We Tied!");
}
else{
System.out.println("Paper! You Win!");
}
}
}

运行时:

--------------------Configuration: Input - JDK version 1.7.0_05 <Default> - <Default>--------------------
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:22: error: not a statement
random.nextInt(10 - 1 + 1) + 1;
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:26: error: illegal start of type
if (name.equalsIgnoreCase("Rock")){
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:26: error: <identifier> expected
if (name.equalsIgnoreCase("Rock")){
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:26: error: ';' expected
if (name.equalsIgnoreCase("Rock")){
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:26: error: illegal start of type
if (name.equalsIgnoreCase("Rock")){
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:26: error: <identifier> expected
if (name.equalsIgnoreCase("Rock")){
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:26: error: ';' expected
if (name.equalsIgnoreCase("Rock")){
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: illegal start of type
if (0<randomInt && randomInt<4){
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: <identifier> expected
if (0<randomInt && randomInt<4){
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: ';' expected
if (0<randomInt && randomInt<4){
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: > expected
if (0<randomInt && randomInt<4){
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: illegal start of type
if (0<randomInt && randomInt<4){
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: '(' expected
if (0<randomInt && randomInt<4){
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: <identifier> expected
if (0<randomInt && randomInt<4){
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: illegal start of type
if (0<randomInt && randomInt<4){
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: <identifier> expected
if (0<randomInt && randomInt<4){
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:28: error: '(' expected
System.out.println("Paper! You Lose!");
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:28: error: illegal start of type
System.out.println("Paper! You Lose!");
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:28: error: ';' expected
System.out.println("Paper! You Lose!");
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:28: error: invalid method declaration; return type required
System.out.println("Paper! You Lose!");
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:28: error: illegal start of type
System.out.println("Paper! You Lose!");
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:30: error: class, interface, or enum expected
else if(4<= randomInt &&randomInt <=6){
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:32: error: class, interface, or enum expected
}
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:35: error: class, interface, or enum expected
}
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:40: error: class, interface, or enum expected
}
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:43: error: class, interface, or enum expected
}
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:46: error: class, interface, or enum expected
}
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:52: error: class, interface, or enum expected
}
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:55: error: class, interface, or enum expected
}
^
C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:58: error: class, interface, or enum expected
}
^
30 errors

Process completed.

最佳答案

第一个问题是您在所有语句中都使用了 randomInt,但您定义了 rn

所以编译器不知道randomInt是什么。

编辑:请修复该问题并发布更多错误。

另一个问题是你在random.nextInt(10 - ....) 语句。

关于java - RandomInt 和 If Else 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31058209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com