How do I put a limit between the ifs? - java

I have a little issue, my code prints the if(RanNum >= 4) and if(RanNum >=8 ) together, but I just want to print (RanNum >=8), I want to put a limit between then, but I don't know how, thank's in advance!
int Min = 1;
int Max = 10;
int RanNum = Min + (int)(Math.random() * (Max - Min) );
System.out.println(RanNum);
if(RanNum <= 3){
System.out.println("hello");
}
if(RanNum >= 4 ){
System.out.println("four four four four four");
}
if(RanNum >= 8){
System.out.println("here it is");
}

You have an issue with your logic. For example if RanNum is greater than 8 then it will also be greater than 4. Use else if you want a branch of code that only runs when the if condition is not satisfied.
Use if else and re order the conditions
if(RanNum <= 3){
System.out.println("hello");
}else if(RanNum >= 8 ){
System.out.println("here it is");
}
else{
System.out.println("four four four four four");
}

Related

Less then or equal to Else IF Java stuck

This program enters in a temperature and based on the temperature a message is outputted. My program is getting stuck tempOutside <= 50 message.. When I enter -10 the output is "Time to turn on the heat". I cannot use && as this is an assignment questions and we have not used this concept.
if (tempOutside > 50)
System.out.println("Enjoy the weather");
else if (tempOutside <= 50)
System.out.println("Time to turn on the heat");
else if (tempOutside <= 30)
System.out.println("Check the gas in you car before leaving");
else if (tempOutside <= -10)
System.out.println("BUNDLE UP it's \"COLD\" outside ");
else
1) if-else must be in numerical order (hi to low)
2) Default must be coded
3) Repeat the code, but reverse the order(low to hi)
All ints are either > 50 or <= 50. The first two conditions match everything.
Change the second condition to
> 30
And the third condition to
> -10
Etc.
it would have been more clear if you could have mentioned your exact question directly and then your implementation as it looks like there could be a misunderstanding of the question here.
But I think perhaps this is what is being asked here??
if (tempOutside > 50)
{
System.out.println("Enjoy the weather");
}
else if (tempOutside <= 50)
{
if(tempOutside <= 30)
{
if(tempOutside <= -10)
{
System.out.println("BUNDLE UP it's \"COLD\" outside ");
}
else {
System.out.println("Check the gas in you car before leaving");
}
}
else
{
//when the temp is between 30 and 50
System.out.println("Time to turn on the heat");
}
}

Calculate the Probability of winning (wins/ (wins + losses)) with 10,000 simulations in the game of craps.. this is part of an assignment

Calculate the Probability of winning (wins/ (wins + losses)) using 10,000 simulations in the game of craps. Here is the method for the game of craps:
public class CrapsGame {
public static void main(String[] args) {
int dice1 = (int)(Math.random()* 6) + 1;
int dice2 = (int)(Math.random()* 6) + 1;
int roll = dice1 + dice2;
System.out.println();
System.out.print("You rolled "+roll+". ");
if(roll == 2 || roll == 3 || roll == 12){
System.out.println("You Lose !");
}else if(roll == 7 || roll == 11){
System.out.println("You Win !");
}else{
System.out.println("Point is "+roll+"\n");
dice1 = (int)(Math.random()* 6) + 1;
dice2 = (int)(Math.random()* 6) + 1;
int roll2 = dice1 + dice2;
System.out.print("You rolled "+roll2+". ");
while(roll2 != 7){
if(roll == roll2){
System.out.println("You Win !");
break;
}else{
System.out.println("Point is "+roll+"\n");
}
dice1 = (int)(Math.random()* 6) + 1;
dice2 = (int)(Math.random()* 6) + 1;
roll2 = dice1 + dice2;
System.out.print("You rolled "+roll2+". ");
}
if(roll2 == 7){
System.out.println("You Lose !");
}
}
}
}
I don't think this should be difficult, I just need code to run 10,000 simulations and then also calculating the probability. Thank you :)
Would it be possible to have someone insert a working version of this
Putting a while or for loop outside of the logic and creating 2 counters (timesWinning, timesLosing). Incrementing each according, inside the existing code. After the loop runs 10.000 times, do the math as needed: wins/ (wins + losses)
thank you this is part of an assignment
And you should modify his code to this at the end:
System.out.println("Probability of winning: " + ((double)timesWon/(timesWon + timesLost)));
I got this as a result from my own:
You played: 10000.0, won: 5078, probability of winning : 0.5078
You played: 1.0E8, won: 50707214, probability of winning : 0.50707214
Your code has already all the logic needed. To repeat it as many times you want, there are different ways. They are known as loops. If you search on Google you will find for, while, do/while and some others (you have a while already inside the code).
For your question, the basic would be to repeat it 10,000 times the same things.
public class CrapsGame {
public static void main(String[] args) {
// Create some variables to control the times it won and times it lost
int timesWon = 0;
int timesLost = 0;
// Here we're saying it's gonna repeat 10000 times, incrementing 1 by 1
for(int i=0; i<10000; i++) {
// Here you will insert all the existing logic from your program
// In the existing code, increment the variables declared according
if(roll == 2 || roll == 3 || roll == 12){
System.out.println("You Lose !");
timesLost++;
}else if(roll == 7 || roll == 11){
System.out.println("You Win !");
timesWon++;
}else{
// Where you find it won, insert `timesWon++;`
// Where you find it lost, insert `timesLost++;`
}
}
// Here it exits the for loop (after 10000 times)
// Here's where you can calculate
System.out.println("Probability of winning: " + (timesWon/(timesWon + timesLost)));
}
}
This should be sufficient to get the desired result.
Hope it helps.

How to check for every second and fifth position in a String?

This code prints yup to every second char (1,3,5,7,9..). Now I'm having a problem because I need also print a word for example " yap " to every fifth char (4,9,14,19...). So every tenth is yup yap etc... Appreciate if you have any hints or solution. Thank you for help!
for(int i = 0; i < word.length(); i++){
if( i % 2 != 0 && i > 0) {
System.out.print(word.charAt(i) + " yup");
System.out.println();
}
else{
System.out.println(word.charAt(i));
}
}
Just add else if statements like
else if (i + 1 % 5 == 0) {
// for every fifth
} else if (i + % 10 == 0) {
// for every tenth
}
and remove an unnecessary check i > 0 in the if statement.

How do I format the logic for 3 conditions and use a previous condition as well?

NOT ASKING FOR ANYONE TO WRITE THE PROGRAM FOR ME
I am having trouble understanding how to setup the logic for a program given these following conditions:
For every day late up to and including 7 days, the late fee is $0.10/day
After 7 days, for every day late up to and including 90 days, the late fee is $0.20/day.
(Example: a book returned 10 days late incurs a late fee of $1.30 late fee, $0.10 for each of the first 7 days plus $0.20 for each of the remaining 3 days)
This is where I am having issues. I am not sure how to include the previous condition along with this one. My best guess is some sort of nested loop or an elseif containing this condition and something about the last one?
After 90 days, an unreturned book is considered lost. All late fees are waived, but the borrower is charged the cost of the book, plus a $10 restocking fee.
This is my best attempt, but I'm not sure this is the most efficient logic:
if (daysLate > 90)
{
costDue = bookPrice + 10;
}
else if (daysLate <= 90)
{
costDue = (7*0.10) * (daysLate*0.20);
}
else if (daysLate <= 7)
{
costDue = daysLate*0.10;
}
else
{
IO.reportBadInput();
}
The second condition should be changed, otherwise the third if won't be reached. Also the third condition should be changed to check if the daysLate variable is greater or equal to zero:
if (daysLate > 90)
{
costDue = bookPrice + 10;
}
else if (daysLate >= 7)
{
costDue = (7*0.10) + ((daysLate - 7) * 0.20); // change here
}
else if (daysLate >= 0)
{
costDue = daysLate*0.10;
}
else
{
IO.reportBadInput();
}
if (daysLate > 90)
{
costDue = bookPrice + 10;
}
else if (daysLate > 7 && daysLate <= 90)
{
costDue = (7*0.10) * (daysLate*0.20);
}
else
{
costDue = daysLate*0.10;
}
Simply update the second clause and drop that last one, that'll straighten it out.
addition to all previous answers - the calculation for mid-term (between 7 days and 90) should have an addition instead of multiplication (7*0.10) + (daysLate*0.20); or even 1.30 + (daysLate*0.20); (as the problem description suggests)
When you use an if statement you have to make sure that it separates the cases that you want to keep separate. In your code, the first else if statement includes all instances where daysLate <= 90, so the second else if would never execute.
Also check your past 7 days due computation, its seems like you would be overcharging people for those first 7 days.
if (daysLate > 90)
{
costDue = bookPrice + 10;
}
else if (daysLate >= 0)
{
costDue = daysLate * 0.10;
if (daysLate > 7) {
costDue += (daysLate - 7) * 0.10;
}
} else {
IO.reportBadInput();
}
Another way to do this, while sacrificing readability a bit, and eliminating one else-if condition:
if (daysLate > 90) {
costDue = bookPrice + 10;
}
else if (daysLate >= 0 && daysLate <= 90) {
costDue = Math.min(7, daysLate) * 0.10 + Math.max(daysLate - 7, 0) * 0.20;
}
else {
IO.reportBadInput();
}

To produce an random output: 100 numbers from 901 to 999

I'm trying to create two separate outputs of 10 columns and 10 rows of numbers. I know I can do the first output using numbers 4 through 7 and the second output using numbers 10 through 90. But I have in trouble to do the third output using numbers 901 through 999. Below is the Java code I have:
import java.util.Random;
public class LabRandom
{
private static final Random rand = new Random();
public static void main(String[] args)
{
int number;
int i = 1;
while (i <= 100)
{
//number = rand.nextInt(4) + 4;
System.out.printf("%-5d", rand.nextInt(4) + 4);
if (i % 10 == 0)
{
System.out.println();
}
i++;
}
System.out.println();
i = 1;
while (i <= 100)
{
//number = rand.nextInt(4) + 4;
System.out.printf("%-5d", rand.nextInt(9)*10+10);
if (i % 10 == 0)
{
System.out.println();
}
i++;
}
System.out.println();
i = 1;
while (i <= 100)
{
//number = rand.nextInt(4) + 4;
System.out.printf("%-5d", rand.nextInt(100)*10);
if (i % 10 == 0)
{
System.out.println();
}
i++;
}
}
}
I'm having trouble understanding what you want. If you want to create an output of 100 randomly chosen number in the range 900 to 999, with line breaks after every 10 such numbers, try adding this loop to your code:
i = 1;
while (i <= 100)
{
// generate a random number from 0 to 100-1,
// then add 900 to transform the range to 900 to 999
System.out.printf("%-5d", rand.nextInt(100) + 900);
if (i % 10 == 0)
{
System.out.println();
}
i++;
}
By the way, if you really want to print numbers 10 to 90, your second loop is incorrect.
Right now you print multiples of 10, from 10 to 90, eg 10,20,30.....90
For every number between, you would want:
rand.nextInt(80)+10
// difference between the highest and the lowest value you want to have in your result
int range = 99;
// the lowest possible value you want to see in your result
int lowestValue = 901;
//note that you will never get the numbers 900 and 1000 this way, only between
int result = rand.nextInt(range) + lowestValue;
You might want to read what exactly nextInt(value) does (easy to do inside a proper IDE since it will provide JavaDoc tooltip, which is of course available and well detailed in such general Java classes).

Categories