Golf score array in Java [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need help on approaching this problem for my CHS Intro to programming class... The problem states:
Writer a program that asks a user how many rounds of golf they played. Using an array, have the user enter what their scores were for each round of golf they played. Sum the users score and display the total to the user.
What I am confused on is the Array part of the problem... Would it be correct for the user input to be the array size? If so how can I code it so that the user is able to input the arrays size. Thanks in advance.

I'd use:
// TODO: Ask how many rounds
...
int total = 0;
int[] scores = new int[numberOfRounds];
for (int round = 0; round < numberOfRounds; round++) {
// TODO: Ask for score
scores[round] = score;
total += score;
}
I'm not sure of the purpose of putting the scores in an array, but if that's what's asked for, I guess you have to!

Related

Less input by the user than asked [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
int[] arr=new int[4];
for(int i=0;i<4;i++)
{
arr[i]=sc.nextInt();
}
Suppose i expected the user to provide 4 inputs but it gave only 3.
Expected= 1 2 3 4
Given= 1 2 3
How do i avoid this error? please help
Exception in thread "main" java.util.NoSuchElement Exception
at java.util.Scanner.throwFor (Scanner.java:862)
at java.util.Scanner.next (Scanner.java: 1371) at Source.main(Source.java:16)```
Whenever you put sc.nextInt(); during runtime JVM will ask for input
i see that you run you for loop four times hence jvm will ask for input four times
it will automatically cast you input to integer variable, if you put any other input it will throw error as
arr[i]=sc.nextInt();
you are storing jvm's user input in an integer array

Is there a general function or way in Java to calculate an equation of the form (a+b+...+n)^2 with a,b,n >= 0? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need to find a way to implement an algorithm in Java that can calculate (a+b+...+n)^2 with a,b,n >= 0. The purpose is to use it afterwards in order to calculate Jain's Fairness index for my algorithm in networks. Is there any standard way to do that or any specific library for advanced math that i might have missed?
Just sum those n values in for loop and then multiply it by itself. Or am i missing something?
The number of possible problems you may encounter is infinite, so you should not be surprised if you often get into a situation where there is no method to help you. Let's suppose that you have an array of numbers, let's suppose it has double elements and the name of the array is input, then:
double sum = 0;
for (int index = 0; index < input.length; index++)
sum += input[index];
double result = Math.pow(sum, 2);
//output holds the result
If there is a possibility of overflow, then you will need to handle it. Also, you will need to handle the validation that your items are positive.

Need help for java to randomly decide between male or female [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am new to coding, and I am currently modding a code. Here is pat of the original.
if ((world[row][col] == ORGANISM) && (check == 2))
worldCopy[row][col] = ORGANISM;
The original code would basically spawn a offspring (a.k.a a *) in a certain location on a grid. However, now I am modding for the offspring to be randomly male or female. How should I write a new piece of subcode so that it would randomly pick for ORGANISM to be male or female? I don't need specifics, just want some help because I am stuck.
Look into the Random class. It provides random generation of ints, and in your case, booleans. I don't know what data type organism is, but something like this should help:
if(new Random().nextBoolean()) {
//Male
} else {
//Female
}
Inside those statements, set the type of organism (I'm guessing that's an enum variable)
Math.random() method can be used which returns a decimal number between 0.0 to 1.0. A check can be done to see if the value is > or < 0.5 and decide based on that.
String gender;
if(Math.random() > 0.5) gender = "MALE";
else gender = "FEMALE";

Java simple for-loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have to find all possibilities to distribute n things to k containers. The containers all should have a different size so I made k inner for-loops for counting every possibility. Sorry for the bad explanation, but my english is not that good.
Now i need to know how to make k for loops so that it works for 2 and for 10.
Thanks
break your problem into 2 steps :
get input 'times' from the user
run the loop 'times' times
Scanner in = new Scanner(System.in);
System.out.println("Enter numebr of times: ");
int times = in.nextInt();
for (int i=0 ; i<times ; i++){
// looping 'times' times
System.out.println("hello");
}

How to get the bet in the play method? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm writing a poker game and finished the Card class, deck Class, checkHands class. Now I'm working on the play class. I defined two static variable at the beginning and three data fields:
private static final int startingBalance=100;
private static final int numberOfCards=5;
// holding current poker 5-card hand, balance, bet
private List<Card> currentHand;
private int balance;
private int bet;
Then, my question is how to get the value of bet depending on the users' input?
bet = input?
Scanner scanner=new Scanner(System.in)
int bet=scanner.nextInt();
JAVA SE7 Scanner
you can use switch case for different inputs provided by the user.
eg.
You can ask the user
enter 1 to bid
enter 2 to fold
and on ,
then use
case 1:
// what ever you wanna do
and on..
you can read about switch here

Categories