Let's try making this code look a little bit nicer too.
Java: | import java.util.Scanner;
public class Percent {
public static void main (String[] args ) {
Scanner input = new Scanner (System. in);
System. out. println("Enter");
int num1 = input. nextInt();
System. out. println("Enter again");
int num2 = input. nextInt();
int total = num1 + num2;
int percent1 = num1 / total * 100;
System. out. println (percent1 );
}
} |
And, how about we break out the prompt logic.
Java: | import java.util.Scanner;
public class Percent {
public static void main (String[] args ) {
Scanner input = new Scanner (System. in);
int num1 = getIntWithPrompt ("Enter", input );
int num2 = getIntWithPrompt ("Enter again", input );
int total = num1 + num2;
int percent1 = num1 / total * 100;
System. out. println(percent1 );
}
static int getIntWithPrompt (String prompt, Scanner s ) {
System. out. println(prompt );
return s. nextInt();
}
} |
|