I'm trying to figure out how to implement a total of my SCORES and VOTES which I have done no problem. Where I'm getting confused and can't seem to solve is how I would go back through my array of Contestants and get their percent of the total SCORES or VOTES. e.g player 1 has a score of 10 out of a total score of 30, player 2 has a score of 15/30 ect... a running average is not a problem its a real total average I can't seem to wrap my head around. Any advice would be terrific.
package contestscore;
import java.util.*;
public class ContestScore {
public static void main(String[] args) {
// TODO code application logic here
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter number of contesants: ");
int numContest = keyboard.nextInt();
System.out.println("Enter contestants DANCE SCORE and NUMBER OF VOTES EARNED: ");
int[] danceScore = new int[numContest+1];
int[] numVotes = new int[numContest+1];
double avgDanceScore = 0.0;
double avgVoteScore = 0.0;
double totalDanceScore = 0.0;
int numContestIndex = 0;
double avgContestDanceScore = 0.0;
int totalVotes = 0;
double avgContestVote = 0.0;
for(numContestIndex = 1; numContestIndex <= numContest; numContestIndex++)
{
danceScore[numContestIndex] = keyboard.nextInt();
numVotes[numContestIndex] = keyboard.nextInt();
System.out.println("DANCE SCORE: "+danceScore[numContestIndex]);
System.out.println("NUMBER OF VOTES: "+numVotes[numContestIndex]);
totalDanceScore = totalDanceScore + danceScore[numContestIndex];
//avgContestDanceScore = danceScore[numContestIndex] /totalDanceScore;
System.out.println("TOTAL DANCE SCORE: "+totalDanceScore);
totalVotes = totalVotes + numVotes[numContestIndex];
//avgContestVote = numVotes[numContestIndex] / totalVotes;
System.out.println("TOTAL CONTESTANT VOTE SHARE: "+totalVotes);
Related
So when I run this it does not add the customer total all together. It just keeps displaying the last item entered as the customer total.
import java.util.Scanner;
public class dinerBill {
public static void main(String[] args) {
double taxRate = 0, customerTotal=0, discountType = 0, grandTotal= 0;
Scanner in = new Scanner(System.in);
String [] itemName = { "0) Soup", "1) Wing", "2) Burger", "3) Chicken Sandwich", "4) Fries", "5) Pie", "6) Ice Cream", "7) Soft drink", "8) Coffee"};
double [] itemPrice= {2.50 , .15 , 4.95, 5.95, 1.99, 2.95, 2.99, 1.50, 1.00};
System.out.println("Enter the number of people in the party");
int numberOfPeople = in.nextInt();
while (numberOfPeople >0) {
System.out.println("Enter 1 if customer recieves teen or eldery discount ");
System.out.println("Enter 2 if the customer recieves no discount");
System.out.println("Enter 3 if the customer is under 5");
discountType = in.nextInt();
if (discountType == 1) {
discountType = .85;
taxRate = 1;
}
if (discountType ==2) {
discountType = 1;
taxRate = 1.05;
}
if (discountType ==3)
discountType = 0;
System.out.printf("%-24s", "Menu");
System.out.print("Prices" + "\n");
System.out.println("--------------------------------");
for (int i = 0; i < itemName.length; i++) {
System.out.printf("%-24.21s" ,itemName[i]);
System.out.print(itemPrice[i] +"\n");
}
System.out.println("Enter the corresponding number");
for (int choices=3; choices > 0; choices--) {
double choicePrice = 0 , customerTotalBeforeDiscount = 0;
System.out.println("Enter customers item");
int customerItem = in.nextInt();
if (customerItem ==1) {
System.out.println("How many wings ordered?");
int wingsOrdered = in.nextInt();
double priceOfWings = wingsOrdered * itemPrice[1];
choicePrice = priceOfWings;}
else
choicePrice = itemPrice[customerItem];
customerTotalBeforeDiscount +=choicePrice;
double customerTotalBeforeTax = customerTotalBeforeDiscount * discountType;
customerTotal = customerTotalBeforeTax * taxRate;
}
System.out.print("The total for the customer is $" );
System.out.printf("%.2f \n" , customerTotal );
grandTotal += customerTotal;
numberOfPeople--;
}
System.out.print("The total is $");
System.out.printf("%.2f", grandTotal);
in.close();
System.exit(0);
}
}
Here are the results that I'm getting:
Enter the number of people in the party
1
Enter 1 if customer recieves teen or eldery discount
Enter 2 if the customer recieves no discount
Enter 3 if the customer is under 5
2
Menu Prices
--------------------------------
0) Soup 2.5
1) Wing 0.15
2) Burger 4.95
3) Chicken Sandwich 5.95
4) Fries 1.99
5) Pie 2.95
6) Ice Cream 2.99
7) Soft drink 1.5
8) Coffee 1.0
Enter the corresponding number
Enter customers item
0
Enter customers item
0
Enter customers item
0
*The total for the customer is $2.6*3
The total is $2.63
Your problem is in
double choicePrice = 0 , customerTotalBeforeDiscount = 0;
because it resets customerTotalBeforeDiscount on each iteration. Instead do:
double customerTotalBeforeDiscount = 0;
for (int choices=3; choices > 0; choices--) {
double choicePrice = 0;
well after looking at it , the customer total did reset to 0, after I fixed that, I noticed that it was =+ and not +=.
customerTotalBeforeDiscount =+ choicePrice;
customerTotalBeforeDiscount += choicePrice;
so yes yours was right but somehow I got the += changed when it was correct in my original post.
As you see this program supposed to give me number of lines.
I want to know how to put a condition give me the minimum triangle side
like if I give the number 5 to the (line) it will loop five times and each line
I have to put 3 separated numbers but I can't find a way to put a condition to give me which one of them is the minimum one.
import java.util.Scanner;
public class Triangles {
private int side[];
public static void main(String args[]) {
double line;
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of Triangles:");
line = s.nextInt();
int side[] = new int[(int) line];
System.out.println("Please, insert lengths of the sides of this triangles(3 real numbers per line) ");
for (int i = 0; i < side.length; i++) {// for reading array
side[0] = (int) s.nextDouble();
side[1] = (int) s.nextDouble();
side[2] = (int) s.nextDouble();
double perimeter = side[0]+side[1]+side[2];
System.out.println(perimeter);
System.out.println("Enter the next Triangles:");
}
line--;
}
}
according to your requirement i have modified it.i hope this will solve your problem, please let me know if you need any help
public static void main(String[] args) {
double line;
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of Triangles:");
line = s.nextInt();
int side[] = new int[3];//because every triangle has 3 sides
double min = Double.MAX_VALUE;//assuming perimeter of a tringle will be grater than zero
while(line-->0){//we need to loop as many no Triangles
System.out.println("Please, insert lengths of the sides of this triangles(3 real numbers per line) ");
// for (int i = 0; i < side.length; i++) {// for reading array
side[0] = (int) s.nextDouble();
side[1] = (int) s.nextDouble();
side[2] = (int) s.nextDouble();
double perimeter = side[0]+side[1]+side[2];
if(perimeter<min){
min = perimeter;
}
System.out.println(perimeter);
//System.out.println("Enter the next Triangles:");
//}
}
System.out.println("Minimum Perimeter :"+min);
// line--;
}
I am trying to take values from separate arrays and perform divisional math. I have created a method to perform the division, but I keep getting the "bad operand..." error. I have searched and searched, but cannot find resolution. I need to be able to take the values from tripMiles and divide that by the values from gallons.
import java.util.Scanner;
public class Week6Challenge {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
int count = 0;
//double miles = 0, gallons = 0;
//Arrays
String[] tripName;
tripName = new String[11];
double[] tripMiles;
tripMiles = new double[11];
double[] tripMPG;
tripMPG = new double [11];
double[] gallons;
gallons = new double [11];
//double miles = 0, gallons = 0;
while (count <= 9){//start while
System.out.println("Enter a description of this trip");
tripName[count] = scan.next();
System.out.println("How many miles did you drive?");
tripMiles[count] = scan.nextDouble();
System.out.println("How many gallons of gas did you use on this trip");
gallons[count] = scan.nextDouble();
count++;
}//end while
tripMPG[count] = answer(tripMiles, gallons);
System.out.println("Trip Name \t Miles Traveled \t MPG");
int k = 0;
for(k = 0; k < 10; k++){
System.out.println(tripName[k]+ "\t\t" + tripMiles[k] + "\t\t\t" + tripMPG[k]);
}
}
public static double answer(double[] num1, double[] num2){
return (num1/num2);
}
}
You are trying to divide two arrays like:
return (num1/num2);
Which is not valid.
Instead if you need length or sum of two arrays and then divide, you could sum up all the elements and then divide the two values.
You can't divide array like this (num1/num2)
Code snippet of one method how to do division of arraya
public static double answer(double[] num1, double[] num2){
//assumimg both array is of equal length
for (int i=0;i<num1.length;i++){
double result = num1[i]/num2[i];
}
}
As already been mentioned you can't divide arrays to each other, but their elements.
Change your answer function so instead of two arrays of double it takes two double and returns the result:
//num1 & num2 are double, not array
public static double answer(double num1, double num2){
return (num1/num2);
}
Remove tripMPG[count] = answer(tripMiles, gallons); from right after the while loop and instead add the following line at the end of your while loop right before count++;:
tripMPG[count] = answer(tripMiles[count], gallons[count]);
So your while should look like this:
while (count <= 9){//start while
System.out.println("Enter a description of this trip");
tripName[count] = scan.next();
System.out.println("How many miles did you drive?");
tripMiles[count] = scan.nextDouble();
System.out.println("How many gallons of gas did you use on this trip");
gallons[count] = scan.nextDouble();
tripMPG[count] = answer(tripMiles[count], gallons[count]);
count++;
}//end while
I need to write a program in Java that computes the average score for 4 students. The student will put in their 4 scores, and once they are done they will input -1 to compute the average. Once this is done the program needs to move onto student 2 and so on. At the end it is supposed to display the highest average from the 4 student average test scores. Here is what it should look like when it is run:
Student 1
Enter your score: 100
Enter your score: 90
Enter your score: 80
Enter your score: 70
Enter your score: -1 * once the student enters -1 it should compute average
Average Score = 85.
Student 2
Enter your score: 90
ETC
ETC
The problem with my code is that the average is only correct for the first student. When I input -1 to get the average for the second student, the calculation is incorrect. We are only allowed to use loops. The only hints I was given were that we are supposed to write an outer loop that iterates 4 times, write an inner loop that loops as long as the student has scores to enter, inside the inner loop prompt the user to enter their last score or -1 to compute average. I don't want you guys to do the project for me but to just set me in the right direction. I feel like I am not using the right loop.
import java.util.Scanner;
public class TestScore
{
public static void main(String[]args)
{
double score = 0;
double totalScore = 0;
double count = 0;
double average = 0;
Scanner input = new Scanner(System.in);
System.out.println("Student 1");
System.out.printf("Enter Your Score: ");
score = input.nextDouble();
while (score != -1){
System.out.printf("Enter Your Score: ");
totalScore = totalScore + score;
score = input.nextDouble();
count++;
average = totalScore / count;
if (score == -1){
System.out.printf("Average Score = %.2f\n ",average);
count = 0;
score = 0;
totalScore = 0;
average = 0;
System.out.println("Student 2");
System.out.printf("Enter Your Score: ");
score = input.nextDouble ();
count++;
average = totalScore / count;
}
}
}
}
You haven't explicitly asked a question so I'll try and comply to the "set me in the right direction" part.
I'd suggest re-formatting the loop structure to a cleaner one, like this:
double total;
for(int student = 1; student <= 4; student++) {
System.out.printf("Student %d\n", student);
double sum = 0, count = 0;
while(true) {
System.out.printf("Enter your score: ");
double input = scanner.nextDouble();
if(input == -1) break;
sum += input;
count++;
}
total += sum;
System.out.printf("Average: %.2f\n", sum / count);
}
System.out.printf("Total: %.2f\n", total);
Hope that's enough to give you some pointers.
edit: forgot to take care of total
So, you wish to iteratively go through all the input and just remember the maximum one. Make an integer variable max and after each student, just change it if needed. (It's zero by default jn Java)
As for the calculation for each student, you shouldn't be checking for the failed " score != - 1" condition in each iteration. Instead, you should do the final calculations after the while loop. (average, possible update of the maximum, resetting the variables, etc. )
You also need the outer loop (in the stated code, these calculations are done for one student only) which you would control in a different manner.
Also, if you need to use only 4 grades, you might want to consider using the for loop.
You can try with this :D
public static void main (String[] args) throws java.lang.Exception
{
double average = 0;
double i = 0;
int student = 0;
boolean flag = true;
Scanner input = new Scanner(System.in);
while(flag)
{
System.out.printf("Student: ");
System.out.println(student);
System.out.print("Enter Your Score: ");
double score = input.nextDouble();
if(score!=-1){
average=average+score;
i=i+1;
}
if(score==-1){
System.out.printf("Average: ");
System.out.println(average/i);
//reset values
average = 0;
i = 0;
student=student+1;
}
if(score==-2){
//you need break the while in some moment.
flag = false;
}
}
}
Basically, the program I am supposed to write is to get the energy usage from the customer for 12 months and then output the total usage, price for two tariffs (the formulas are included in the code) and say which tariff is cheaper. But it also has to check whether the input for each of those 12 months is within the range (greater than "0" AND less or equal to "1000").
I have found a fairly easy(?) way to do it using arrays, however I have no idea how to check whether each one of the integers scanned to be in that array are actually within the range 0 < int <= 1000
If the integer is less than 0 or greater than 1000, the program has to output a line "Please enter a valid amount" and ask for the same integer again, so that it doesn't store the wrong value, if it makes sense?
import java.util.Scanner;
public class EnergyConsumptionExample {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int total_usage;
float t1_cost, t2_cost;
final int MAX_USAGE = 1000, MIN_USAGE = 0;
int[] energyCons = new int[12];
for (int month = 0; month < energyCons.length; month++) {
System.out.print("Please enter the monthly kWh usage for month ");
System.out.print((month + 1) + ": ");
energyCons[month] = scan.nextInt();
}
int totalCons = 0;
for (int month = 0; month < energyCons.length; month++) {
totalCons += energyCons[month];
}
System.out.println();
System.out.println("Total usage for the year was " + totalCons + " kWh");
t1_cost = (float) (totalCons * 0.1);
t2_cost = (float) ((totalCons * 0.09) + 50);
System.out.println("Using tariff one, the cost is: " + t1_cost);
System.out.println("Using tariff two, the cost is: " + t2_cost);
System.out.println();
if (t1_cost > t2_cost) {
System.out.println("Tariff two would be cheaper for this customer.");
} else {
System.out.println("Tariff one would be cheaper for this customer.");
}
}
}
Change your input reading loop to something like this:
for (int month = 0; month < energyCons.length; month++) {
System.out.print("Please enter the monthly kWh usage for month ");
System.out.print((month + 1) + ": ");
int inputValue = scan.nextInt();
while (inputValue < 0 || inputValue > 1000) {
System.out.println("Please enter a valid amount: ");
inputValue = scan.nextInt();
}
energyCons[month] = inputValue;
}