I'm getting a weird output from running this simple java program.
The output is: 0 4 2 -6
Why does the x++ print 0, it should be printing 4.
import java.util.*;
import java.io.*;
public class Java1 {
public static void main(String[] args) throws IOException {
int x = 4;
int y = -5;
System.out.println(x++ + " " + func(x++, y) + " " + --y);
}
public static int func(int work, int y) {
int z = work + y;
work++;
y++;
System.out.print(z + " ");
return z + work + y;
}
}
Okay, here is what's going on: First x++ is evaluated, returning 4 (which is later printed) and leaving x at 5. Then x++ is evaluated again, passing 5 to func. Then func is evaluated with 5 and -5 parameters. In here z is 0 (5 + (-5) = 0) which is then printed (BEFORE the println in the main method. func then returns 2 (0 + 6 + (-4)) which is also added to the string. Finally --y results in -6. Now the println in the main method prints its string (4 2 -6).
func(x++, y) is executed first, so 0 comes from System.out.print(z + " "); in func.
System.out.print(z + " ");
is executed before
System.out.println(x++ + " " + func(x++, y) + " " + --y);
So the 0 comes from z, not x.
I have mentioned the flow of points from 0 to 7 in the comments
public static void main(String[] args) throws IOException {
int x = 4;
int y = -5;
System.out.println(x++ + " " + func(x++, y) + " " + --y);
// thus 0]4 6]2 (value returned as z) 7] localvalue of --y as -6
}
//1] x++ makes x as 5 when it is passed to func()
public static int func(int work, int y) {
int z = work + y;
//2] z = 5 + -5 = 0
work++;
//3] work which was x as 5 is now 6
y++;
//4] y will be -4 now
System.out.print(z + " ");
return z + work + y;
//5] z = 0 + 6 + -4 = 2 and is returned to func() caller
}
import java.util.*;
import java.io.IOException;
public class Java1
{
public static void main(String args[])
{
int x = 4;
int y = -5;
System.out.println("x = "+ (x++ ) +" func = "+ (func(x++, y) ) + " y = "+ --y);
}
public static int func(int work, int y)
{
int z = work + y;// 5+-5 = 0
work++; //6
y++; //-4
System.out.print("Z = " + z + " ");//0
return z + work + y; //0 + 6+-4 = 2
}
}
OUTPUT :
Z = 0 x = 4 func = 2 y = -6
Here the func() is executed first and hence the value of variable z is printed as 0 and then the x++ value is printed as 4.
Related
Given a number between 1 and 30, find all three-number combinations that sum up to this number and return the amount of combinations without using loops.
for example, given 5, print
1 + 1 + 3
1 + 2 + 2
1 + 3 + 1
2 + 1 + 2
2 + 2 + 1
3 + 1 + 1
This is what I have right now, using Java.
private static int numbers(int num, int num1, int num2, int num3){
boolean valid_solution = num1+num2+num3 == num;
int counter = 0;
if (valid_solution){
System.out.println(num1+" + "+num2+" + "+num3);
counter++;
}
if (num1>10 || num2>10 || num3>10 || num1+num2+num3>num){
return counter;
}
counter += numbers(num, num1 + 1, num2, num3)+numbers(num, num1, num2 + 1, num3)+numbers(num, num1, num2, num3 + 1);
return counter;
}
public static int solutions(int num){
if (num < 0 || num > 30) return 0;
return numbers(num, 1, 1, 1);
}
I seem to get duplicates, for example for 5-
3 + 1 + 1
2 + 2 + 1
2 + 1 + 2
2 + 2 + 1
1 + 3 + 1
1 + 2 + 2
2 + 1 + 2
1 + 2 + 2
1 + 1 + 3
edit - I'm also not allowed to use global variables.
You can see why the duplicates are happening if you add a little logging
1:1:1
2:1:1
3:1:1
3 + 1 + 1
4:1:1
3:2:1
3:1:2
2:2:1
2 + 2 + 1
3:2:1
2:3:1
2:2:2
2:1:2
2 + 1 + 2
3:1:2
2:2:2
2:1:3
1:2:1
2:2:1
2 + 2 + 1
3:2:1
2:3:1
2:2:2
1:3:1
1 + 3 + 1
2:3:1
1:4:1
1:3:2
1:2:2
1 + 2 + 2
2:2:2
1:3:2
1:2:3
1:1:2
2:1:2
2 + 1 + 2
3:1:2
2:2:2
2:1:3
1:2:2
1 + 2 + 2
2:2:2
1:3:2
1:2:3
1:1:3
1 + 1 + 3
2:1:3
1:2:3
1:1:4
counter:9
so since you are making recursive calls on incrementing numbers, when you're recursively calling num2+1, you want to make sure that num1 is less than or equal to num2 in order to avoid duplicates
I have a suspicions that this code of mine is far from being a good solution, but who knows, may be it will be helpful for you in some way.
public class FindSumCombinations {
static int start = 5;
static int comb1 = 0;
static int comb2 = 0;
public static void main(String[] args) {
comb1(start);
}
public static int comb1(int x){
if(x == 0) return 0;
comb1 = x;
comb2(x);
return comb1(x-1);
}
public static int comb2(int x){
if(x == 0) return 0;
comb2 = x;
comb3(x);
return comb2(x-1);
}
public static int comb3(int x){
if(x == 0) return 0;
if(x + comb2 + comb1 == start){
System.out.println(comb1 + "+" + comb2 + "+" + x);
System.out.println(x + "+" + comb1 + "+" + comb2);
System.out.println(comb2 + "+" + x + "+" + comb1);
}
return comb3(x-1);
}
}
This question already has answers here:
Java: sum of two integers being printed as concatenation of the two
(10 answers)
Closed 5 years ago.
This is my java code
public class exercise {
public static void main(String[] args) {
int x = 8;
int y = 4;
System.out.println("x + y = " + x + y);
System.out.println("x * y = " + x * y);
System.out.println("x + x + y =" + x + x + y);
double z = x / y;
System.out.println("z = " + z);
}
}
it's supposed to look like this:
x + y = 12
x * y = 32
x + x + y = 20
z = 2.0
but when I run it with eclipse this is the result I get:
x + y = 84
x * y = 32
x + x + y =884
z = 2.0
As you can see 8 + 4 definitely != 84
As well as 8 + 8 + 4 != 884
It looks like the eclipse typed the values 8 and 4 in the first line, and didn't add them together, the same thing with the third line it just typed 8 and 8 and 4, not adding them together.
Do you have any idea how to fix this problem?
You need to put brackets around arithmetic operation
System.out.println("x + y = " + (x+y));
System.out.println("x * y = " + (x*y));
System.out.println("x + x + y ="+( x+x+y));
public class Review {
public static void main(String[] args) {
int x = 10, y = 5;
System.out.println(" " + x + y); // string + x + y
}
}
How does this equal 105 rather then 15?
What makes it different from the below code?
public class Review {
public static void main(String[] args) {
int x = 10, y = 5;
System.out.println(x + y); //Only x + y
}
}
" " + x + y means " " + x then + y
so the first code that you write we can divide into 2 steps:
1. " " + x => " " + 10 => "10"
2. "10" + y => "10" + 5 => "105"
But the second is just a number + number, so we get the number result.
I'm not a native English speaker, and I'm learning, sorry about my bad English, And I hope this is helpful.
The first answer the type is String because of the " " and you are using string concatenation by using the +
With the second one, the + is not being used to concatenate with a String.
When you mix a primitive data type with a String in System.out.println(), the Java compiler assumes that you want every data type to be converted to a String object. Adding brackets around your primitive data types allows you to calculate first before conversion as Jim Garrison suggested: System.out.println(" " + (x + y));
This question already has an answer here:
Java Division error
(1 answer)
Closed 7 years ago.
I was trying to create a probability calculator for fun, but for some reason, java gets the incorrect answer when I divide two numbers. Here is my code....
import javax.swing.JOptionPane;
public class ProbabilityCalculator {
public static void main(String args[]) {
String a = JOptionPane.showInputDialog("One out of.....");
int x = Integer.parseInt(a);
int numLoops = 1000;
int y = 0;
int n = 0;
for (int i = 0; i < numLoops; i++) {
int result = (int) (Math.random() * x + 1);
int result2 = (int) (Math.random() * x + 1);
if (result == result2)
y++;
else
n++;
}
System.out.println(y);
System.out.println(numLoops);
System.out.println(y/numLoops);
double d = (y/numLoops) * 100; //get it? double d??
JOptionPane.showMessageDialog(null, "Out of " + numLoops + " trials, "
+ y + " times it worked, while " + n + " times it didn't.");
JOptionPane.showMessageDialog(null, "Your percentage was " + d
+ "%.");
System.exit(0);
}
}
When I ran this code one time, y was 514, numLoops was 1000, but d would be 0, when d is supposed to be 51.4 (514 / 1000 * 100). Why is this happening?
y/numLoops will be an integer since both arguments are ints. Try (double)y/numLoops or y/(double)numLoops instead.
If you decompose double d = (y/numLoops) * 100; you'll get something similar to those steps:
int r = y/numLoops; - according to the spec an operation having two integer operands will have an int result.
double d = r * 100 here r will be 0 due to being int.
So in the following set of code I don't understand why "%.1f" will not round y to 1 decimal, I get the following when running the program:
123 F = Exception in thread "main"
java.util.IllegalFormatConversionException: f != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4045)
at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2761)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2708)
at java.util.Formatter.format(Formatter.java:2488)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at Check03A.main(Check03A.java:17)
I also tried Math.round(y * 10) / 10 but it gives me for example 29.0 instead of 28.9
import java.util.*;
import java.io.*;
import java.lang.*;
import type.lib.*;
public class Check03A
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
PrintStream print = new PrintStream(System.out);
print.println("Enter the temperature in Fahrenheit");
int x = scan.nextInt();
int y = 5 * (x - 32) / 9;
print.printf(x + " F = " + "%.1f", y + " C");
}
}
The problem is here:
print.printf(x + " F = " + "%.1f", y + " C");
There are two arguments to this method:
x + " F = " + "%.1f" (the format string),
y + "C" (the argument)
Condensing the first argument, the statement becomes:
print.printf(x + "F = %.1f", y + "C");
The problem: y + "C". Why? Well, one argument of the + operator is a String; therefore, this + becomes a string concatenation operator and what the argument will ultimately be is String.valueOf(y) + "C".
But the format specification in the format string is%.1f, which expects a float or double, or their boxed equivalents. It does not know how to handle a String argument.
Hence the error.
There is also the problem that you are doing an integer division, but this is not the topic of this question. Provided that all numeric problems are solved, your printing statement will ultimately be:
print.printf("%.1f F = %.1f C", x, y);
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
PrintStream print = new PrintStream(System.out);
print.println("Enter the temperature in Fahrenheit");
int x = scan.nextInt();
//change it
float y = 5 * (x - 32) / 9;
print.printf(x + " F = " + "%.1f", y).print(" C");
}
Make y as float if you want to use %f
float y = 5 * (x - 32) / 9;
print.printf(x + " F = " + "%.1f", y ).print("C");