This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Find the max of 3 numbers in Java with different data types (Basic Java)
Write a program that uses a scanner to read three integers (positive) displays the biggest number of three. (Please complete without using either of the operators && or ||. These operators will be covered in class shortly. Similarly loops are not required.)
Some sample run:
Please input 3 integers: 5 8 3
The max of three is: 8
Please input 3 integers: 5 3 1
The max of three is 5
import java.lang.Math;
import java.util.Scanner;
public class max {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please input 3 integers: ");
String x = keyboard.nextLine();
String y = keyboard.nextLine();
String z = keyboard.nextLine();
int max = Math.max(x,y,z);
System.out.println(x + " " + y + " "+z);
System.out.println("The max of three is: " + max);
}
}
I want to know what's wrong with this code and how I can find the max when I input 3 different values.
Two things: Change the variables x, y, z as int and call the method as Math.max(Math.max(x,y),z) as it accepts two parameters only.
In Summary, change below:
String x = keyboard.nextLine();
String y = keyboard.nextLine();
String z = keyboard.nextLine();
int max = Math.max(x,y,z);
to
int x = keyboard.nextInt();
int y = keyboard.nextInt();
int z = keyboard.nextInt();
int max = Math.max(Math.max(x,y),z);
You should know more about java.lang.Math.max:
java.lang.Math.max(arg1,arg2) only accepts 2 arguments but you are
writing 3 arguments in your code.
The 2 arguments should be double,int,long and float but your are
writing String arguments in Math.max function. You need to parse them in the required type.
You code will produce compile time error because of above mismatches.
Try following updated code, that will solve your purpose:
import java.lang.Math;
import java.util.Scanner;
public class max {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please input 3 integers: ");
int x = Integer.parseInt(keyboard.nextLine());
int y = Integer.parseInt(keyboard.nextLine());
int z = Integer.parseInt(keyboard.nextLine());
int max = Math.max(x,y);
if(max>y){ //suppose x is max then compare x with z to find max number
max = Math.max(x,z);
}
else{ //if y is max then compare y with z to find max number
max = Math.max(y,z);
}
System.out.println("The max of three is: " + max);
}
}
It would help if you provided the error you are seeing. Look at http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html and you will see that max only returns the max between two numbers, so likely you code is not even compiling.
Solve all your compilation errors first.
Then your homework will consist of finding the max of three numbers by comparing the first two together, and comparing that max result with the third value. You should have enough to find your answer now.
Related
This program is supposed to print the numbers (indiviual digits) in a number
`
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int number = sc.nextInt();
int Size = 0;
String Conversion = Integer.toString(number);
Size = Conversion.length();
int n = 0;
while (n <= Size){
int d = number%10;
int power = Size - 2;
number = (number/(10^(power)));
System.out.println(d);
n += 1;
}
}
}
`
Really Appreciate for Anyone for Took time to help me.
Thanks
For some reason I get
1
9
3.
instead of
1
3
4
using the debugger gives me some hint, Specifically this block `
number = (number/(10^(power)));
`
for second iteration the value is +4 than expected, 3.
for third Its okay.
changing and adding +4 on that block gives
1
3
7
4
Found it !!
Credit OH GOD SPIDERS, tkausl
Solution 1 : Instead of using carrot characters in
number = (number/(10^(power)));
use Math.pow function.
Solution 2 :
Don't use (number/(10^(power))
instead just divide by 10
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 5 years ago.
I want to generate a random number in Java. It can be of integer, byte or float type, but all I really need it is to generate a random number. This is what I'm doing:
Generate a random number within a certain range (e.g. 5 through 20).
Take the number and store it within a variable.
Perform arithmetic on it.
Here's the code:
import java.util.HashMap;
public class Attack {
public static void main(String[] args) {
HashMap<String, Integer> attacks = new HashMap<String, Integer>();
attacks.put("Punch", 1);
attacks.put("Uppercut", 3);
attacks.put("Roundhouse Kick", 5);
int actionPoints = // Code for random number generation
System.out.println("A brigade integrant appeared!");
System.out.println("What do you do?");
System.out.println("1: Punch [1 AP], 2: Uppercut [3 AP], 3: Roundhouse Kick [5 AP]");
System.out.println("You have " + actionPoints + " Action Points.");
Scanner reader = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = reader.nextInt();
reader.close();
if n == 1 {
System.out.println("The brigade integrant takes 2 HP of damage!");
}
else if n == 2 {
System.out.println("The brigade integrant takes 5 HP of damage!");
}
else if n == 3 {
System.out.println("The brigade integrant takes 8 HP of damage!");
}
}
}
In Java 1.7+ you can do it in one line (not counting the import statement ;):
import java.util.concurrent.ThreadLocalRandom;
int actionPoints = ThreadLocalRandom.current().nextInt(5, 21); // 5 to 20 inclusive
Try this :
int lower = 12;
int higher = 29;
int random = (int)(Math.random() * (higher-lower)) + lower;
There are multiple options for you to generate a random number. Two of these would be:
Math.random(); // Random values ranging from 0 to 1
Random rand; rand.nextInt(x); // Random int ranging from 0 to x
To specify the exact range you could do something like this:
int RandomNumber = Min + (int)(Math.random() * Max);
package Review_Randomnum;
import java.util.Scanner;
public class RandomNum {
public static void main(String[] args) {
System.out.println("Please enter a minimum value: ");
Scanner input = new Scanner(System.in);
int first = input.nextInt();
System.out.println("Please enter a maximum value: ");
int second = input.nextInt();
int number = ((int)(Math.random()) * ((second - first)) + first);
System.out.println(number);
}
}
So I've checked numerous answers on this website, and to no avail, I don't get any errors, I just get the minimum value printed. I don't know how to get the program to produce a random integer between the two values input
Your random number is being truncated to 0 in the following expression:
((int)(Math.random())
The reason for this is that Math.random() returns a double greater than or equal to 0 and less than 1, which when cast to an int will be truncated to 0 (see the documentation for more information).
Instead, compute your desired number first as a double, and then cast back to an int:
int number = (int)(Math.random() *(second - first) + first);
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'm trying to read in two integer numbers from the console and then display a random integer number generated between these two numbers.
this is my code.
import java.awt.*;
import java.io.*;
import java.util.*;
import java.math.*;
public class Question8{
public static void main(String[] args)
{
int first, second;
Scanner myScanner = new Scanner(System.in);
System.out.println("Enter first integer: ");
int numOne;
numOne = myScanner.nextInt();
System.out.println("You have keyed in " + numOne);
System.out.println("Enter second integer: ");
int numTwo;
numTwo = myScanner.nextInt();
System.out.println("You have keyed in " + numTwo);
Random generator = new Random();
//int num = generator.nextInt(numOne) + numTwo;
System.out.println("Random number: " + numOne + generator.nextInt(numTwo - numOne));
}
}
after i execute this program..
i do not get the random number that's between the 2 integers.
this is my output:
Enter first integer:
20
You have keyed in 20
Enter second integer:
30
You have keyed in 30
Random number: 204
hope you guys can help me in any way you can (:
Make sure that first is always the smaller value and use new Random().nextInt(second - first) + first;. first is in the range of possible generated numbers, second is exclusive.
A random generator (Method in java.lang.Math?) will generate a random double x, where 0 <= x <= 1. Just multiply x with the range and add any offset to the product.
NOTE: (I do not need anyone to write the whole program for me, I only need the algorithm!)
I need to create a program that prompts the user to enter two integers. The program then needs to list all the even numbers in between the two inputed integers and output the sum. And then the same for the odd numbers. (using While loops)
I will then need to rewrite the code to use a do-while loop, and then rewrite it AGAIN using a for loop.
Here is an example of what the result should look like:
Enter an integer: 3
Enter another integer larger than the first: 10
Even Numbers: 4, 6, 8, 10
Sum of even numbers = 28
Odd Numbers: 3, 5, 7, 9
Sum of odd numbers = 24}
I tried starting off with the even numbers with something like this, but it just gets stuck at the first number, even if the first number is even.
import java.util.Scanner;
public class EvenOddSum_While {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a number: ");
int num1 = keyboard.nextInt();
System.out.println("And another: ");
int num2 = keyboard.nextInt();
while (num1 < num2){
while (num1 %2 == 0){
System.out.print(num1 + ", ");
num1++;
}
}
}
}
The inner while has no end criteria, you need if there instead. Also, your num1++ Statement must be in the outer while loop, not the inner one.
Also, there is no real algorithm here, you're struggling with the language itself ;)
General advice: either run through your code with a step-by-step debugger, virtually every IDE has one OR place excessive log /System.out.println statements in your code to understand what it's doing
When you said 'in between' did you mean including the two integers? Because you did include them. Okay. So do this.
int x = 0;
int y = 0;
int even = 0;
int odd = 0;
int evenx = 0;
int oddx = 0;
int evena = 0;
int odda = 0;
Scanner scan = new Scanner(System.in);
//Prompts the user to input the first number
x = scan.nextInt();
//Prompts the user to input the second number
y = scan.nextInt();
for(int i = x;i<y;i++,x++;){
if(x%2 = 0){
even = even + x;
evenx++;
}
if(x%2 = 1){
odd = odd + x;
oddx++;
}
}
evena = even/evenx;
odda = odd/oddx;
//print it out. There. The algorithm.
God. Do this site have auto-format?