I want to write a program that can read scores, get the best scores, and assign grades based on that. I have the general idea so far, my problem is figuring out how to incorporate the necessary for-loops. Another problem I have is figuring out how to change line
----(String s1=input.next();) and have the final answer print out the number of entries entered.
I must also use the equation
Grade is A if best score if >= best-10
Grade is B if best score if >= best-20
Grade is C if best score if >= best-30
Grade is D if best score if >= best-40
Ex solution:
Enter number of Students: 4
Enter 4 scores: 40 55 70 58
Student 0 score is 40 and grade is C
Student 1 score is 55 and grade is B
etc. to 4 students.
public class Lab_7_1{
public static void main(String[] args){
java.util.Scanner input=new java.util.Scanner(System.in);
System.out.print("Enter the number of students: ");
int n1=input.nextInt();
int [] number=new int[n1];
System.out.print("Enter "+n1+" Scores: ");
String s1=input.next();
}
}
Try something like this:
public class Lab_7_1{
public static void main(String[] args){
java.util.Scanner input=new java.util.Scanner(System.in);
System.out.print("Enter the number of students: ");
int n1=input.nextInt();
int [] number=new int[n1];
System.out.print("Enter "+n1+" Scores: ");
for (int i = 0; i < number.length; i++) {
number[i]=input.nextInt();
int b=i+1;
System.out.println("Score "+b+": "+number[i]);
}
int best = number[0];
for (int i = 0; i < number.length; i++) {
if (number[i]>best) best = number[i];
}
System.out.println("The best score is: "+best);
for (int i = 0; i < number.length; i++) {
if(number[i]>=best-20 && number[i]<=best-10) System.out.println("For score "+number[i] +" you get B grade.");
if(number[i]>=best-30 && number[i]<=best-20) System.out.println("For score "+number[i] +" you get C grade.");
if(number[i]>=best-40 && number[i]<=best-30) System.out.println("For score "+number[i] +" you get D grade.");
}
}
}`
Next step (using a for loop):
for (int i = 0; i < n1; i++) {
number[i] = input.nextInt();
}
Now that you've learned how to write a for loop, you can loop through the numbers to find the highest number, then do it again to print the results.
Related
The program needs to ask the user how many courses it took which is the number of elements in the array. Then it needs to identify which grades are below 65 and print those and then identify which grades are above 90 and print those. Right now, the output just prints 0,1,2,3,4,5 after asking the user for how many courses they took and their grade in each of the course.
import java.util.Scanner;
import java.util.ArrayList;
public class StudentApplication {
public static void main(String[] args) {
Scanner Number = new Scanner(System.in);
System.out.print("How many courses did you take during the school year: ");
int x= Number.nextInt();
int grades[] = new int[x];
for (int courses =1; courses<=x; courses++) {
System.out.print("Enter your grade for that course: ");
Number.nextInt();
int y = Number.nextInt();
}
for (int counter = 0; counter < grades.length; counter++) {
if (counter < 65) {
System.out.print(counter);
}
if (counter >90) {
System.out.print("\n"+counter);
}
}
}
}
Is this what u want?
Scanner sc = new Scanner(System.in);
System.out.print("How many courses did you take during the school year? : ");
int takeCoursesNum = sc.nextInt();
int grades[] = new int[takeCoursesNum]; // array number start "0" ~ ex) { [0], [1], [2] }
for (int i = 0; i < takeCoursesNum; i++) {
System.out.print("Enter your grade for that course : ");
int grade = sc.nextInt();
grades[i] = grade;
}
for (int i = 0; i < takeCoursesNum; i++) {
if (grades[i] < 65) {
System.out.println("below 65 : " + i);// System.out.println : auto change line. no need \n.
}
if (grades[i] > 90) {
System.out.println("above 90 : " + i);
}
}
I hope it is. Happy Codinging :)
I'm very new to java and I'm wondering on how to stop the user from entering an input after the 10th. (inputs are separated by space)
sample output would be:
Please type 10 temperatures in Celsius: 30 12 20.5 ..
Temperatures in Celsius: 30.0 12.0 20.5 …
Temperatures in Fahrenheit: 86.0 53.6 68.9 …
Here's my code:
import java.util.Scanner;
public class array {
public static void main(String args[]) {
double [] a = new double [10];
Scanner sc=new Scanner(System.in);
System.out.print("Please type 10 temperatures in Celsius: ");
for(int j=0; j<10; j++)
a[j] = sc.nextDouble();
System.out.print("Temperatures in Celsius: ");
for (int i=0;i<a.length;i++)
System.out.print(a[i] + " ");
System.out.print("\nTemperatures in Fahrenheit: ");
for (int i = 0; i < a.length; i++)
a[i] = a[i]*1.8+32;
for (int i = 0; i < a.length; i++) {
System.out.printf("%.1f ", a[i]);
}
}
}
This is not possible, because input is provided from a terminal/shell, and this terminal usually reads input linewise. You have to do much more low level coding to handle scenarios like these.
Just output an error if the users enters more than 10 values and let him enter them again.
Daniel is correct, with your current setup its not quite possible. You could do it, if you're ok with the console looking a bit different. This requires more 'Enter' presses, and moves each temp to its own line.
Enter 10 temps:
Enter 1: [user input here]
Enter 2: [user input here]
etc etc
System.out.println("Enter 10 temps");
for(int i = 1; i <= 10; i++){
System.out.print("Enter " + i + ":");
a[i-1] = scanner.nextDouble();
}
And you can do some thing like this evry time put a number fo 10 times for each will print temperture in Celsius and in Fahrenheit
public static void main(String args[]) {
int counter = 0;
double[] a = new double[10];
Scanner sc = new Scanner(System.in);
System.out.print("Please type temperatures in Celsius: ");
while(counter < 10){
a[counter] = sc.nextDouble();
System.out.println("Temperatures in Celsius: " + a[counter]);
double tF = a[counter]*1.8 + 32;
System.out.println("Temperatures in Fahrenheit: " + tF);
counter++;
}
}
I am creating a program that calculates users grades based on their input. All is good except that I need to deal with users inputting invalid entries.
Example: The user enters three exam scores on a single line (95 90 87 for example). If one of these scores is negative (95 90 -87) I need the program to NOT enter those three scores and assign a letter grade and instead prompt the user to re-enter the scores correctly.
Here is my code:
public class GradeCalculator {
public static void main(String[] args) {
int classSum = 0; // variable used to hold sum of entire classes exams
int classExams = 0; // variable used to hold number of exams taken by whole class
Scanner s = new Scanner(System.in);
System.out.println("Welcome to Gradecalculator!");
System.out.println("Please enter the number of students:");
int students = s.nextInt();
System.out.println("Please enter the number of exams:");
int exams = s.nextInt();
int i = 0;
int studentnumber = 1;
int sum = 0;
while (i < students) { // loop until it matches number of students entered above
i++;
sum = 0;
System.out.println("Enter student " + studentnumber++ + "'s name :");
String studentname = s.next();
System.out.println("Enter exam scores :");
int input = 0;
for (; input < exams; input++) {
int n = s.nextInt();
sum+=n;
if (n < 0) {
System.out.println("Invalid exam scores, reenter: "); //if one of the scores entered is negative, display message
}
}
double average = sum/exams; // assign letter grade based on average of exams
if (average <= 100 && average >= 90) {
System.out.println("Letter grade: A");
System.out.println(studentname + " gets 4 stars! ****");
} if (average <= 89 && average >= 80) {
System.out.println("Letter grade: B");
System.out.println(studentname + " gets 3 stars! ***");
} if (average <= 79 && average >= 70) {
System.out.println("Letter grade: C");
System.out.println(studentname + " gets 2 stars! **");
} if (average <= 69 && average >= 60) {
System.out.println("Letter grade: D");
System.out.println(studentname + " gets 1 star! *");
} if (average <= 59) {
System.out.println("Letter grade: F");
System.out.println(studentname + " gets 0 stars!");
}
classSum += sum; // add sum of this student's scores to the classSum
classExams += exams; // add exams taken by this student to amount of exams taken by whole class
}
int classAverage = classSum/classExams; // compute class average
System.out.println("Class statistics:");
System.out.println("\tAverage: " + classAverage);
}
}
Clearly I have this code in the wrong place:
if (n < 0) {
System.out.println("Invalid exam scores, reenter: "); //if one of the scores entered is negative, display message
}
}
Because this is the output I get when entering a negative score:
Enter exam scores :
70 70 -70
Invalid exam scores, reenter:
Letter grade: F
joe gets 0 stars!
As you can see, it still assigns a letter grade and stars and asks for the next student's name. I need it to instead ask to re-enter this student's scores. Can't figure out how to do that.
You can subtract one from the input inside that for loop so that it stays in that loop and other inputs aren't wiped out (and also make sure that the sum doesn't add those negative inputs)
int input = 0;
for (; input < exams; input++) {
int n = s.nextInt();
if (n>=0)
sum+=n;
else {
input--;
System.out.println("Invalid exam score entered, reenter: ");
}
}
EDIT: Use interactive input, which should be more user friendly.
You need to reset your loop counter to start the loop again:
// Delete first output and counter variable initialization before loop
for (int input = 1; input < exams; input++) {
System.out.println(String.format("Enter %d. exam score: ", input + 1));
int n = s.nextInt();
if (n < 0) {
System.out.println("Invalid value (must be positive)!");
input--; // Reenter last value
} else {
sum+=n; // Only use correct scores.
}
}
I need a program that should add x numbers. The numbers should come from user input so I need some sort of loop. I have gotten as far as shown below, but I'm stuck since I have no idea how to add a new number without deleting the previous?
System.out.println("How many numbers to use?");
int number = keyboard.nextInt();
for (int i = 0; i<number ; i++) {
System.out.println("whats the number");
double first = keyboard.nextDouble();
}
If all you need is the average, you don't need to keep all the numbers you get from user input. Just keep one variable that holds their sum.
define the sum variable outside the loop (initialized to 0), and add to it each number you get from user input.
int number = keyboard.nextInt();
double sum = 0;
for (int i = 0; i<number ; i++)
{
System.out.println("whats the number");
sum += keyboard.nextDouble();
}
double average = sum / number;
public class Average {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double sum = 0;
int num;
System.out.println("enter how many num");
num = sc.nextInt();
System.out.println("please enter " + num + " numbers");
for (int i = 0; i < num; i++) {
sum += sc.nextDouble();
}
double avg = sum / num;
System.out.println("Average of " + num + " numbers is:" + avg);
}
}
This question already has answers here:
Getting the lowest and highest value from integers without using arrays?
(2 answers)
Closed 9 years ago.
I need this code to print out the lowest score entered and I'm sitting here for hours and I can't figure out how to do it.I know how to make it using arrays but I need to use for loops.Do you have any hints?
Example output:
How many scores? 3
Enter score 1: 90
Enter score 2: 80
Enter score 3: 100
Lowest score is 80
My code:
int score,numScore,count;
Scanner keyboard = new Scanner(System.in);
System.out.print("How many scores? ");
numScore = keyboard.nextInt();
for(count = 1; count <= numScore; count++){
System.out.print("Enter score " + count + ":");
score = keyboard.nextInt();
}
Maintain a variable that you compare against with each keyboard input.
int lowestScore = 100000;
for(count = 1; count <= numScore; count++){
System.out.print("Enter score " + count + ":");
score = keyboard.nextInt();
if (score < lowestScore) {
lowestScore = score;
}
}
Maintain a variable to find the lowest score like this
int lower = -1;
Inside for loop
if(lower == -1) {
lower = score;
} else if(score < lower) {
lower = score;
}
Finally lower has the required value
Simply save the lowest score in a variable and update it every time the lower score is encountered:
int lowestScore = 100;
for(count = 1; count <= numScore; count++){
System.out.print("Enter score " + count + ":");
score = keyboard.nextInt();
if(score < lowestScore){
lowestScore = score;
}
}
Added 3 lines
int score,numScore,count;
int minimum;
Scanner keyboard = new Scanner(System.in);
System.out.print("How many scores? ");
numScore = keyboard.nextInt();
for(count = 1; count <= numScore; count++){
System.out.print("Enter score " + count + ":");
score = keyboard.nextInt();
if (count == 1) minimum = score;
if (score < minimum) minimum = score;
}