Whats wrong with the program?
// Question 1: is there any way i can shorten the main and put it in sub? //Question 2: although the output is what i want, it feels like im doing or explaining something wrong.
public class Example2 {
public Example2(String aname){
name=aname;
count=0;
count++;
//constructor}
public static void entername(){
System.out.println("Please Enter Your Name");
}
// This is a static method that will run right when the program is rans.
public void message(){
System.out.println("Your name is "+name);
System.out.println("Total registered:"+count);}
// This is a instance method that uses instance field”name,count”.
private String name;
private int count;
}
Main:
import java.util.Scanner;
import java.util.Random;
public class Example2Tester {
public static void main(String[] args) {
Example2.entername(); //Class.method
Scanner input = new Scanner(System.in); /*creates an object for Scanner to run on*/
String i = input.next(); //stores the name you typed in i
Example2 name=new Example2(i);//creates object for name to be stored
Random r= new Random(); //creates object for random to run on
int d=r.nextInt(9999); /*sets the limit from 0to9999 intergers,
assign the random number as your ID*/
System.out.println("Your ID is : "+d);
name.message(); //object.method
}
}
Output:
Please Enter Your Name
asdf
Your ID is : 8677
Your name is asdf
Total registered:1
//Thanks alot
Re: Whats wrong with the program?
Quote:
Originally Posted by
fishy8158
Whats wrong with the program?
How about you first tell us -- what is wrong with it? Or are we supposed to guess?
Re: Whats wrong with the program?
well, did i comment anything wrong on the main? if not, then how can i put some more of the code from main to sub? thanks for replying.