有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java修复int转换并为类添加一个返回

我已经完成了一个代码,但是有几个问题需要解决,但是,我尝试了许多不同的方法都不起作用,有人能在这里给我一些帮助吗

我的错误部分是total = getScore;return total;

。。。意思是不用担心,我相信这些是不必要的,也不正确的

 public class Main {
     public static void main(String[] args) {
         int total;
         boolean winLoss;
         int win = 0;
         int loss = 0;
         int point = 0;
         for(int i=0; i<100000;i++){
             total = getScore(); 
             if...
             ...
             while(true){
                 total = getScore();
                 if(total == point){...
                      ...
                 }   
             }
        ...
     private static void getScore(){
         int dice1 = (int)(Math.random()*(6-1)+1);
         int dice2 = (int)(Math.random()*(6-1)+1);
         int total = dice1+dice2;
         return total;
     }
}

共 (2) 个答案

  1. # 1 楼答案

    程序是错误的。怎么样?有一个方法返回类型为void,用于填充局部变量值

    void getScore()

  2. # 2 楼答案

    函数getScore()的类型应为int

    public class run {
          public static void main(String[] args) {
            int total;
            boolean winLoss;
            int win = 0;
            int loss = 0;
            int point = 0;
            for(int i=0; i<100000;i++){
                total = getScore(); 
                if...
                ...
                while(true){
                    total = getScore();
                    if(total == point){...
                        ...
                    }   
                }
                ...
            }
          private static int getScore(){
             int dice1 = (int)(Math.random()*(6-1)+1);
             int dice2 = (int)(Math.random()*(6-1)+1);
             int total = dice1+dice2;
             return total;
          }
    }