This is a tool that you could use to generate an order for positions in any circumstance.
However, I am encountering infinite loops. How can I fix this?
import java.util.Random;
import java.util.Scanner;
public class WhoGoesFirst {
public static void main(String args[]) {
Random random = new Random();
Scanner input = new Scanner(System.in);
int MIN = 1;
int students = 0;
System.out.print("How many students do you have?");
students = input.nextInt();
int comp = random.nextInt(students - MIN + 1) + MIN;
for (int number = 0; number <= students; comp++) {
System.out.println(random);
}
}
}
Increment your number variable in the loop and it should fix the issue:
for (int number = 0; number <= students; number++, comp++) {
System.out.println(comp); //random is an object
}
You need to increment number:
import java.util.Random;
import java.util.Scanner;
public class WhoGoesFirst {
public static void main(String args[]) {
Random random = new Random();
Scanner input = new Scanner(System.in);
int MIN = 1;
int students = 0;
System.out.print("How many students do you have?");
students = input.nextInt();
int comp = random.nextInt(students - MIN + 1) + MIN;
for (int number = 0; number <= students; number++) {
System.out.println(random);
comp++;
}
}
}
Look at this line:
System.out.println(random);
This implementation is trying to print the random number generator, not the numbers themselves. There is a much easier solution, using the built-in Collections.shuffle:
List<Integer> positions = new ArrayList<Integer>(students);
for (int number = 0; number < students; number++)
positions.add(number);
Collections.shuffle(positions);
for (Integer number : positions)
System.out.println(number);
Related
This is my first time using this and I'm having a bit of trouble with arrays. I need to check if the values in an array that the user enters are continuously being squared such as {2, 4, 16, 256}. Checking if num[1] = num[0] * num[0], num[2] = num[1] * num[1] and so on. Any help would be appreciated.
import java.util.Scanner;
public class PS6Square {
public static void main (String [] args){
Scanner scan = new Scanner(System.in);
System.out.println("How many values would you like to enter?");
String input = scan.next();
int array = Integer.parseInt(input);
int num[] = new int[array];
int i = 0;
boolean result = false;
for(i = 0; i < num.length; i++)
{
System.out.print("Enter a value:\t");
num[i] = scan.nextInt();
if(num[i] == num[i] * num[i])
{
result = true;
}
}
System.out.println("\nThe result is:\t" + result);
}
}
This question has already been asked. Try the search function next time.
But here is a solution:
double sqrt = Math.sqrt(int_to_test);
int x = (int) sqrt;
if(Math.pow(sqrt,2) == Math.pow(x,2)){
//it is a perfect square
}
This is basically a tool teachers would use to generate random numbers for the position everyone is in for presentations perhaps.
It keeps creating infinite loops. What am I doing wrong? Thank you.
import java.util.Random;
import java.util.Scanner;
public class WhoGoesFirst {
public static void main(String args[]) {
Random random = new Random();
Scanner input = new Scanner(System.in);
int MIN = 1;
int students = 0;
System.out.print("How many students do you have?");
students = input.nextInt();
int comp = random.nextInt(students - MIN + 1) + MIN;
for (int number = 0; number <= students; comp++) {
System.out.println(random);
}
}
}
Your number doesn't change in a loop. Try this:
for (int number = 0; number <= students; number++) {
int comp = random.nextInt(students - MIN + 1) + MIN;
System.out.println(comp);
}
package practiceit;
import java.util.Scanner;
public class numbers {
public static void main (String[]args) {
int max=0;
int min = 0;
}
public static int smallestLargest() {
Scanner console= new Scanner(System.in);
System.out.print("how many numbers do you want to enter?");
int num= console.nextInt();
for (int i=1; i<=num; i++) {
int nums =console.nextInt();
System.out.println("Number "+i+": "+nums);
}
return nums;
}
}
How can I return nums so that it is stored as both a max and min variable, so I can print the highest and lowest value typed by the user?
So something like this?
import java.util.Scanner;
public class numbers
{
public static void main(String[] args)
{
int min = 0, max = 0, current, nums;
Scanner scan = new Scanner(System.in);
System.out.println("How many numbers do you want to enter: ");
nums = scan.nextInt();
for(int i = 0; i < nums; i++)
{
current = scan.nextInt();
if(i == 0)
{
min = current;
max = current;
}
if(current < min)
min = current;
if(max < current)
max = current;
}
//after the loop you should have the max and min value the user entered.
System.out.println("The max number is: "+max);
System.out.println("The min number is: "+min);
scan.close();
}
}
You will have all the value in array
package practiceit;
import java.util.Scanner;
public class numbers {
int[] array = null;
public static void main (String[]args) {
int max=0;
int min = 0;
}
public static int smallestLargest() {
Scanner console= new Scanner(System.in);
System.out.print("how many numbers do you want to enter?");
int num= console.nextInt();
array[] = new int[num];
for (int i=1; i<=num; i++) {
int nums =console.nextInt();
System.out.println("Number "+i+": "+nums);
array [i]= nums;
}
return nums;
}
}
If you use Array.sort(array); you will have sorted array and it will be easy to read minimum and maximum value
import java.util.*;
public class Project2Main {
public static void main(String args[]) {
Scanner kb = new Scanner(System.in);
int numSets = 0;
System.out.println("How many sets of dice would you like to roll?");
numSets = kb.nextInt();
kb.nextLine();
RollingDice roller = new RollingDice();
List<List<Integer>> diceSets = roller.rollSets(numSets);
for (List<Integer> diceRolls : diceSets) {
Integer total = sum(diceRolls);
Integer lowest = Collections.min(diceRolls);
System.out.println("Your roll total is: " + total + " and the lowest roll was a: " + lowest);
}
kb.close();
}
public static int sum(List<Integer> list) {
int sum = 0;
for (int number : list)
sum = sum + number;
return sum;
}
}
import java.util.*;
public class RollingDice {
private Random rand;
public RollingDice() {
this.rand = new Random();
}
public List<Integer> roll4d6DropLowest() {
List<Integer> retList = new ArrayList<Integer>();
for (int i = 0; i < 4; i++) {
retList.add(rand.nextInt(6) + 1);
}
retList.remove(Collections.min(retList));
return retList;
}
public List<List<Integer>> rollSets(int numSets) {
List<List<Integer>> results = new ArrayList<List<Integer>>();
for (int i = 0; i < numSets; i++) {
results.add(roll4d6DropLowest());
}
return results;
}
}
Hello Stackoverflow, I am having a small issue with this program. I am trying to make a program to roll four dice and add the three highest rolls together and then print the lowest of those THREE rolls. I made the mistake of of taking the three highest rolls, adding them together, and then printing out the lowest of the four.
How can I fix this issue? I did a lot of googling to figure out how to use lists and collections, but I can't find anything to fix this issue.
Also, how can I make it to where I force the user to select a set and to reroll that set?
Basically what I want to do is have each set assigned to a number (Set 1 is 1, Set 2 is 2, etc.), and then the user types in a number correlating to the number of sets they have, and then the lowest of the three highest rolls is re rolled.
Any help is greatly appreciated.
Using a List of Lists is a slight case of overbombing here, how about this one:
public class Project2Main {
private static Random rand = new Random(System.currentTimeMillis());
public static void main(String args[]) {
int numSets = 4; //set via Scanner, if wanted
List<Integer> dices = new ArrayList<Integer>(numSets);
for (int c = 0; c < numSets; c++) {
int roll = rand.nextInt(6) + 1;
dices.add(Integer.valueOf(roll));
System.out.println(roll);
}
int[] pair = roll4d6DropLowest(dices);
System.out.println("lowest of three highest: " + pair[0]);
System.out.println("sum of 3 highest is: " + pair[1]);
}
/**#param diceRolls Array of dice rolls
* #return return array of two ints:
0: lowest of three highest
1: sum of three highest */
private static int[] rollXd6DropLowest(List<Integer> array) {
int sum = 0;
int low = 0;
Collections.sort(array);
for (int c = (array.size() - 1); c > 0; c--) {
sum += (int) array.get(c);
if (c == 1) {
low = (int) array.get(c);
}
}
int[] pair = { low, sum };
return pair;
}
}
I admit, returning a int[] is not the best of all solutions, but for that little piece of code it should be ok.
Note the initialisation of Random by current time millis to ensure randomness.
The trick is to store the dice rolls in a List of Integers and sort them according to their "natural ordering" with the help of Collections. Highest is topmost.
import java.util.Scanner;
public class People
{
public static final Scanner keyboard = new Scanner(System.in);
static final int Max= 50;
public static void main(String[]args)
{
int index;
int check;
int infants = 0, children = 0, teens = 0, adults = 0;
System.out.println("Please enter value:");
int [] peopleTypes = new int[Max];
for(index=0; index<Max; index++)
{
peopleTypes[index] = keyboard.nextInt();
if(peopleTypes[index] == 1)
infants = infants + 1;
if(peopleTypes[index] == 2)
children = children + 1;
if(peopleTypes[index] == 3)
teens = teens + 1;
if(peopleTypes[index] == 4)
adults = adults + 1;
else
index = index-1;
System.out.print("");
}
So basically I have to create a code that allows a user to input 50 values (1,2,3,4) into a array and count how many of each was inputted and outputs that. I am stuck on creating the code to count the inputted values. I am new to array and am not sure if I'm on the right path.
Actually you don't need to store the input, take a (closer) look at a possible solution:
import java.util.Scanner;
public class People {
static final Scanner keyboard = new Scanner(System.in);
static final int MAX = 50;
static final String[] LABLES = {"infants", "children", "teens", "adults"};
public static void main(String[] args) {
int[] counts = new int[4];
for (int i = 0; i < MAX; i++) {
System.out.println("Please enter value:");
int current = keyboard.nextInt(); //problem input: "abc" -> java.util.InputMismatchException
while (current > 4 || current < 1) {//checks range
System.out.println("Please enter a valid value (between 1 and 4):");
current = keyboard.nextInt();//...and repeat
}
counts[current - 1]++;//increment counter, "current - 1", since 0-based array and 1-based input
}
System.out.println("Evaluation finished!");
for (int i = 0; i < 4; i++) {//print the output
System.out.println(LABLES[i] + ": " + counts[i]);
}
}
}
I maybe misunderstanding the context but I do not see the need for creating a peopleTypesArray. My understanding is that all you need is a count or teens , adults etc from a list of 50 people, so just keep incrementing the individual counters and display them outside the loop. Something like:
for(int i = 0 ; i < 50; i++){
int peopleType = keyBoard.nextInt();
if(peopleType == 0){
infantCount++;
}
---- and so on
}
System.ou.println(" infant = " + infantCount);
--- and so on