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 2 years ago.
Improve this question
I am learning Java, at a very beginning level. I am trying to print the Dice result, so from 1 to 6.
//instance of random class
Random DICE = new Random();
int DiceRange = 7;
int RIGHT = DICE.nextInt(DiceRange);
int LEFT = DICE.nextInt(DiceRange);
System.out.println("Right Dice = " + RIGHT);
System.out.println("Left Dice = " + LEFT);
The issue with this code that "Zero" is also printed sometimes. I want to keep the range from 1 to 6, instead of 0 to 6.
I tried to do the following, but did not work:
int i = 0;
while (DiceRange == 0); {
i++;
}
The CPU went to 100% :)
So how to exclude zero from this?
From Random documentation:
public int nextInt(int bound)
Returns a pseudorandom, uniformly distributed int value between 0
(inclusive) and the specified value (exclusive)
So starting from your code you can solve your problem using :
Random DICE = new Random();
int DiceRange = 6;
int RIGHT = DICE.nextInt(DiceRange) + 1;
int LEFT = DICE.nextInt(DiceRange) + 1;
Related
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 2 years ago.
Improve this question
Example:
Input:
140
Output:
1 4 0
I wanted to make it divide by 100 so that the outcome will be 1, and then divide by 10 and the outcome will be 4 and then by 1 and the answer will be 0. But I am not sure how I am able to achieve it. I also want to use recursion in the method.
You can see an int as a String
int n = 140;
String s = String.valueOf(n);
for(int i = 0; i<s.length(); i++){
System.out.println(s.charAt(i));
}
With no need of recursion.
With recursion it could be something like (i haven't tried it so it could not work):
public String separateInteger(int n){
if(n < 10){
return String.valueOf(n);
}
else{
int mod = n%10;
int quot = n/10;
return String.valueOf(mod) + separateInteger(quot);
}
}
I hope have answered your question. :)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am making a coin flip simulator, but I have a problem with the random values. I need to make a random value that changes every time the while-loop starts again.
System.out.println("How many times do I flip the coin?");
int repetition;
int heads = 0;
int tails = 0;
Scanner amount_of_times = new Scanner(System.in);
repetition = amount_of_times.nextInt();
double luck = (Math.random());
do {
if (luck > 0.5)
heads++ ;
else
tails++ ;
repetition --;
} while (repetition > 0);
System.out.println("The amount of heads was " + heads + ", meanwhile the amount of tails was " + tails + ".");
amount_of_times.close();
}
}
You assign a value to luck only once, before the loop. If you want it to get a different value in each iteration, you should move the assignment to it inside the loop:
do {
double luck = Math.random(); // Here!
if (luck > 0.5)
heads++ ;
else
tails++ ;
repetition --;
} while (repetition > 0);
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 4 years ago.
Improve this question
I have some integer values like 1447948,21163176,95999 and I wanna make them like that:
1447948--> 1400000
21163176-->21000000
95999-->95000
How can I make this with using java?
Math is your friend.
int magnitude = (int) Math.pow(10, Math.log10(n) - 1)
int o = (int) Math.floor(n / magnitude) * magnitude
where n is the input number and o is the output number.
Because rounding is something that is count from the right, you cannot use it, you can just pass from string and use a basic regex to replace the non-2 first digits by 0 :
int val = 1447948;
int res = Integer.valueOf((""+val).replaceAll("(?<=\\d{2})\\d", "0"));
// res : 1400000
(?<=\\d{2})\\d match the digits that have two digits before them
Workable Demo
You can do it for any number by treating it as a string:
int number = 1447948;
String number1 = String.valueOf(number);
String[] split = number1.split("");
StringBuilder number2 = new StringBuilder();
for (int i = 0; i < split.length; i++) {
if(i > 1)
number2.append("0");
else
number2.append(split[i]);
}
int result = Integer.parseInt(number2.toString());
System.out.println(result);
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 5 years ago.
Improve this question
I have this recursion problem that I spent hours on but unable to transform some of my thoughts into code. I have a problem where I need to build a function that tries to fit 3's and 1's into a given number(n). I try to get n from user but then I cannot go further. Basically I need to calculate the possibility of 3's and 1's that add up to n. I know that a constant solution is that I can fit 1n times into n.
Any help is appreciated. Thanks in advance
public static void main(String[] args) {
// TODO code application logic here
Scanner ask = new Scanner(System.in);
System.out.println("input number: ");
int n = ask.nextInt();
}
public static int getPossibility(int n, int k, int p) {
if (n == 0) {
return 0;
} else if (n / p == n ) {
return n;
} else {
int ctr = n;
for (int i = 1; i <= n; i++) {
ctr += getPossibility(n, k - 1, p - 1) ;
}
return ctr;
}
}
}
I'm not sure if I understood what you meant. With this code you try to figure out how many combinatios start by 1 and how many start by 3. Then you just need to sum up and you'll get your solution.
public int possibilities (int n){
int pos1=0;
int pos3 =0;
if (n>2){
pos3 = possibilities(n-3);
}
if (n>0) {
pos1 = possibilities(n-1);
}
return pos1 + pos3;
}
I'm not sure what you want to do, but if you want to calculate how many times 3s and 1s fit into a number you can do it like this:
int threes = n / 3;
int ones = n % threes * 3;
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 6 years ago.
Improve this question
I want to write a java program to find the largest square number which is less than or equal to a given number.
For example when I insert 10 as the inputted number, the answer is 9 because 9is the largest square up to 10
I understand loops, but I can't figure out the logic behind this one.
How about something like the following to get you going ...
double x = 20;
double root = Math.sqrt(x);
int t = (int)root;
System.out.println("Root is:" + root + " and answer is :" + t*t);
Here is how to do this with loops
int n=10; //This is your Number
int i = 0;
for(i=n;i>=1;i--)
if((int)Math.sqrt(i)*(int)Math.sqrt(i)==i)
break;
System.out.println(i);
Below is how it works:-
The Loop runs from the n, which is your number, to 1. It then checks whether, the square root of i, which is running through n to 1, is a perfect square. If it is, it breaks the loop, and prints the value of i on the screen.
public static void main(String argv[]){
System.out.println(largestSquareLessOrEqualTo(145));
}
public static int largestSquareLessOrEqualTo(int limit){
int i = 0;
for (; i <(int)Math.sqrt(limit); i++){
}
return(i*i);
}