Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
87 views
in Technique[技术] by (71.8m points)

java - using 3 classes to implement a program error

What I am trying to accomplish here is launching a program using three separate classes and I seem to get errors. I am relatively new to this and not entirely comprehend the java language yet. So please go easy on me and feel free to give me additional advice on anything that may be conducive to hone my skill.

package com.weightroom;

public class Baeyul { //?? ??
    
    String name,gender;
    int [] threemax = new int [3];
    int weight,total,rank;

}

The second class

package com.weightroom;

import java.util.Scanner;

public class Record {

    int inwon;
    
    Baeyul[] bae;
    
    Scanner sc= new Scanner(System.in);
    
    public void set() {
        
        do {
            System.out.print("?? ?? ");
            inwon= sc.nextInt();
        }while(inwon<1||inwon>5);
        
        bae= new Baeyul[inwon];
    
    }
    
    public void input() {
        
        String[] title = {"???","???","???"};
        
        //???
        
        for(int i=0;i<inwon;i++) {
            
            bae[i] = new Baeyul();
            
            System.out.print((i+1)+ "?? ??? ");
            bae[i].name = sc.next();
        
            System.out.print("??? ");
            bae[i].gender = sc.next();
            
            System.out.print("???? ");
            bae[i].weight = sc.nextInt();
            
            for(int j=0;j<3;j++) { //????
                
                System.out.print(title[j]);
                bae[i].threemax[j] = sc.nextInt();
                bae[i].total+= bae[i].threemax[j];
            }
            
        }       
    }
        
        private void ranking() {
            
            int i,j;
            
            for (i=0;i<inwon;i++) {
                bae[i].rank=1;
        }
            
        for(i=0;i<inwon-1;i++) {
            for(j=1;j<inwon;j++) {
                
                if(bae[i].total>bae[j].total) {
                    bae[j].rank++;
                }else if(bae[i].total<bae[j].total) {
                    bae[i].rank++;
                }
            }
        }
    }   
        
        public void print() {
            
            ranking(); 
            
            for(int i=0;i<inwon;i++) {
                System.out.printf("%6s", bae[i].name);
                System.out.printf("%4s", bae[i].gender);
                System.out.printf("%4d", bae[i].weight);
                
                for(int j=0;j<3;j++) {
                    System.out.printf("%4d",bae[i].threemax);
                }
                
                System.out.printf("%4d",bae[i].total);
                System.out.printf("%4d|n",bae[i].rank);
                
        }
    }
}

*The third class

package com.weightroom;

public class RecordUse {

    public static void main(String[] args) {
        
        Record li= new Record();
        
        li.set();
        li.input();
        li.print();

    }

}
launch
     jamesException in thread "main"    1 100java.util.IllegalFormatConversionException: d != [I
        at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
        at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2793)
        at java.util.Formatter$FormatSpecifier.print(Formatter.java:2747)
        at java.util.Formatter.format(Formatter.java:2520)
        at java.io.PrintStream.format(PrintStream.java:970)
        at java.io.PrintStream.printf(PrintStream.java:871)
        at com.weightroom.Record.print(Record.java:83)
        at com.weightroom.RecordUse.main(RecordUse.java:11)
question from:https://stackoverflow.com/questions/65952127/using-3-classes-to-implement-a-program-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In the print method you haven't put the index of the threemax array, So when you try to print the array in a %4d format, it throws an exception.

Change threemax as threemax [ j ].

Like below

Use

 for(int j=0;j<3;j++) {
      System.out.printf("%4d",bae[i].threemax[j]);
 }

Instead of

for(int j=0;j<3;j++) {
      System.out.printf("%4d",bae[i].threemax);
}

in the print method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...