generate random num thats less than or greater than “x” [duplicate] - java

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 7 years ago.
i am mking a program where the computer guesses your number. and it has to guess a random num. but how do i make the random line be lower or higher num than the perviously num guessed?
package compguse;
import java.util.*;
public class Compguse {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
String a;
String b;
String c;
String ans;
String d;
int input =1;
System.out.println("do u have your number?");
a = scan.nextLine();
while (a.equalsIgnoreCase("yes"))
{
int ran = (int) Math.floor(Math.random()*100)+1;
System.out.println(" is" +ran +" your num?");
a = scan.nextLine();
if(a.equalsIgnoreCase("no"))
{
System.out.println("Was i too high or low?");
b = scan.nextLine();
if(b.equalsIgnoreCase("high"))
{
int ran1 = (int) Math.floor(Math.random() < (int) ran);
}
}
}
}

Just make a while loop which implements this pseudocode:
For example if you want it to be less than the guess:
while random is greater than guess
random = new random number

Related

How can I display a float value to 2 decimal place in JAVA using JOptionpane? [duplicate]

This question already has answers here:
Convert a number to 2 decimal places in Java
(4 answers)
Closed 4 months ago.
I was given a task to calculate the average mark of unknown number of tests in JAVA, I was able to solve the problem but not thoroughly because my output has more than 2 decimal point. May I please be helped to only show my output rounded off to two decimal places. (I am a first year students and I don't know much in JAVA). Here's a snippet of my code.
import java.util.Scanner;
import javax.swing.JOptionPane;
public class number2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number_Of_Tests = 0;
int total_marks = 0;
int mark;
System.out.print("Enter the test mark: ");
mark = scan.nextInt();
while (mark >= 0) {
number_Of_Tests = number_Of_Tests + 1;
total_marks = total_marks + mark;
System.out.print("Enter the test mark: ");
mark = scan.nextInt();
}
float average = total_marks / (float) number_Of_Tests;
JOptionPane.showMessageDialog(null, "The average is: " + average, "AVERAGE",1);
scan.close();
}
}
let's say my input is: 25 , 26 and 28 then 0, my output should be 26.34 but instead I get 26.333334
import java.util.Scanner;
import javax.swing.JOptionPane;
public class number2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number_Of_Tests = 0;
int total_marks = 0;
int mark;
System.out.print("Enter the test mark: ");
mark = scan.nextInt();
while (mark >= 0) {
number_Of_Tests = number_Of_Tests + 1;
total_marks = total_marks + mark;
System.out.print("Enter the test mark: ");
mark = scan.nextInt();
}
float average = total_marks / (float) number_Of_Tests;
JOptionPane.showMessageDialog(null, "The average is: " +"% .2f" + average, "AVERAGE",1);
scan.close();
}
}
The first example is a formatter that always produces two decimal places. The second one only produces as many decimal places (up to 2) as needed. Usage: DF_TWOPLACES.format(average);
final private static DecimalFormat DF_TWOPLACES = new DecimalFormat("0.00", DecimalFormatSymbols.getInstance(Locale.US));
final private static DecimalFormat DF_TWOPLACES = new DecimalFormat("0.##", DecimalFormatSymbols.getInstance(Locale.US));

Multiplication proctoring program

I am working on a project for school and am at a loss for ideas after searching online and looking at loop flow charts, and different methods to accomplish what is required.
The requirements are:
Generate two, 1 digit integers, and ask the user to find the product of said two.
If wrong, print "No. please try again. " & prompt the user for input to try again forever until correct.
If correct, print "Very good! " & prompt question with two new integers.
I am confused as to what kind of loops to use, and how to order them in order to serve their purpose.
import java.util.Scanner; // Program utlizes scanner
import java.security.SecureRandom; // Program utlizes SecureRandom
public class WaringComputerAided {
static void callNumbers() { //method to generate random numbers
SecureRandom rand = new SecureRandom();
Scanner input = new Scanner(System.in);
int num1 = rand.nextInt(10); //generates random integer #1
int num2 = rand.nextInt(10); //generates random integer #2
System.out.printf("How much is "+num1+" times "+num2+" ?: "); // Asks question
int sum = (num1 * num2);
}
static void tryAgain() {
Scanner input = new Scanner(System.in);
System.out.printf("No. Please try again. ");
int answer = input.nextInt();
}
public static void main(String[] args) {
SecureRandom rand = new SecureRandom();
Scanner input = new Scanner(System.in);
int num1 = rand.nextInt(10);
int num2 = rand.nextInt(10);
int sum = (num1 * num2);
do { // Asks question first time, and asks new question when user inputs correct answer
callNumbers(); //Generates numbers and asks new question
int answer = input.nextInt(); // Asks user for input
} while(answer == sum);
if (answer != sum) { // When answer incorrect
tryAgain();
}
if (answer == sum) { // When answer is correct
System.out.printf("Very good! ");
}
} // End method main
} // End class product
First of all your callNumbers method is not returning anything change it to int and return the sum
static int callNumbers() { //method to generate random numbers
SecureRandom rand = new SecureRandom();
// Scanner input = new Scanner(System.in);
int num1 = rand.nextInt(10); //generates random integer #1
int num2 = rand.nextInt(10); //generates random integer #2
System.out.printf("How much is "+num1+" times "+num2+" ?: "); // Asks question
int sum = (num1 * num2);
return sum;
}
At the tryAgain method we need to change the logic if it is incorrect then just call main method to repeat the procedure
static void tryAgain() {
System.out.printf("No. Please try again. ");
main(new String[]{});
}
We want to get the correct answer so we save it to a new variable so then we can compare it with the number user has entered
sum = callNumbers();
The do while logic
do {
sum = callNumbers();
answer = input.nextInt();
if (answer != sum) { // When answer incorrect
tryAgain();
}
if (answer == sum) { // When answer is correct
System.out.printf("Very good! ");
}
} while(answer == sum);
All the code
import java.util.Scanner; // Program utlizes scanner
import java.security.SecureRandom; // Program utlizes SecureRandom
public class WaringComputerAided {
static int callNumbers() { //method to generate random numbers
SecureRandom rand = new SecureRandom();
// Scanner input = new Scanner(System.in);
int num1 = rand.nextInt(10); //generates random integer #1
int num2 = rand.nextInt(10); //generates random integer #2
System.out.printf("How much is "+num1+" times "+num2+" ?: "); // Asks question
int sum = (num1 * num2);
return sum;
}
static void tryAgain() {
System.out.printf("No. Please try again. ");
main(new String[]{});
}
public static void main(String[] args) {
SecureRandom rand = new SecureRandom();
Scanner input = new Scanner(System.in);
int num1 = rand.nextInt(10);
int num2 = rand.nextInt(10);
int sum = 0;
int answer = 0;
do {
sum = callNumbers();
answer = input.nextInt();
if (answer != sum) { // When answer incorrect
tryAgain();
}
if (answer == sum) { // When answer is correct
System.out.printf("Very good! ");
}
} while(answer == sum);
}
}
OUTPUT
How much is 9 times 1 ?: 23
No. Please try again. How much is 6 times 0 ?: 6
No. Please try again. How much is 4 times 3 ?: 2
No. Please try again. How much is 7 times 1 ?: 7
Very good! How much is 0 times 0 ?: 0
Very good! How much is 4 times 4 ?:

Create a program that will continuously accept number when user does not input same number as the previous inputted number [duplicate]

This question already has answers here:
Need To Take Input To Array Until User Enters 0 JAVA
(3 answers)
Closed 5 years ago.
This is the only program that i can't create. Please help me with this.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i = 0, j=0;
do {
System.out.print("Enter number: ");
i = input.nextInt();
i=j;
}
while (i == j);
System.out.println("You have inputted the same number on the previous.");
}
Cheers m8, good luck at your test :D
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i = 0, j = 999999, input = 0;
do {
i = j;
System.out.print("Enter number: ");
input = input.nextInt();
j=input;
} while (i != j);
System.out.println("You have inputted the same number on the previous.");
}

Finding the average of student [duplicate]

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 7 years ago.
I am trying to find the average number of students per class but when I test my program, it only prints 1 or 0 everytime. Any help would be appreciated.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Please enter the number of Students: ");
int s = reader.nextInt();
System.out.println("Please enter the number of classes: ");
int c = reader.nextInt();
int numbStud1 = 0;
int numbClass1 = 0;
double averages =calcAverage (numbStud1 + c,numbClass1 + s) ;
System.out.println("The average is: " + averages);
}
public static double calcAverage(int numbStud, int numbClass){
double average1;
average1 = numbStud / numbClass;
return average1;
}
}
The issue is this:
double averages =calcAverage (numbStud1 + c,numbClass1 + s) ;
Replace it with exchanging c with s:
double averages =calcAverage (numbStud1 + s,numbClass1 + c) ;
And if you want avrage more precision use :
average1 = (numbStud*1.0 / numbClass*1.0);

Creating a method with a for loop to find odd abundant sigma function numbers

I'm trying to create a method that will take a number and determine whether the number is an odd, abundant number with the sigma function. An abundant number is any number that when put into the sigma function generates a sum greater than the number given.
For instance, sigma(12) is abundant because sigma(12) = 1+2+3+4+6+12 = 28. However, it is not odd, so my method would not consider it. I can't figure out why my loop function isn't working , because when I try to input a range it spits up a bunch of number gibberish. Here's what I have so far:
import java.util.*;
public class OddAbundant {
static Scanner input = new Scanner(System.in);
public static void findOddAbundant(){
System.out.println("Please enter the start of the range you want to test for odd abundant integers");
int startRange = input.nextInt();
System.out.println("Please enter the end of the range you want to test for odd abundant integers");
int endRange = input.nextInt();
for(int b = startRange; b <= endRange; b++) {
if (Sigma.Sigma(b)<(b*2))
continue;
else{
if (b % 2 == 1)
System.out.println(b);
}
}
}
public static void main(String[] args) {
findOddAbundant();
}
}
I go through the loop and I can't figure out what's going wrong. I've tested the sigma method, which I can provide if it will help you guys, and it does spit out the correct value when given an integer. Thoughts?
Here is my sigma function:
import java.util.*;
public class Sigma {
static Scanner input = new Scanner(System.in);
public static int Sigma(int s){
int a = 0;
for(int i=1;i<=s;i++){
if(s%i==0)
a = a + i;
}
System.out.print(a);
return a;
}
public static void main(String[] args) {
System.out.println("Please enter the number you want to perform the sigma function on");
int s = input.nextInt();
Sigma.Sigma(s);
System.out.print(" is the sum of all the divisors of your input" );
}
}
The silly problem it was; remove the print statement from Sigma function.
public static int Sigma(int s){
int a = 0;
for(int i=1;i<=s;i++){
if(s%i==0)
a = a + i;
}
System.out.print(a); //why do you have it here?
return a;
}

Categories