im having a little problem about my codes
public class ex{
public static void main(String[] args) {
int sum,int a = 1,int b = 2;
int c = 1,int d = 2;
if (a<b) {
sum = sum+1;
}
if (c<b) {
sum = sum+1;
}
System.out.println("output :"+sum);
}
}
I wanted to add a value of 1 in the int sum if the conditions are met. but its not compiling
the output should be like this:
output: 2
First things first.. If you are a beginner to Java, this is an advise for you to learn well about the syntax of Java declaration, initialization and usage.
Declaration:
If you want to declare variables separately, you have to do it as below:
int a;
int b;
int c;
If you want to declare multiple variables in a single line, you have to do it as below:
int a,b,c;
Initialization:
If you want to initialize multiple variables in a single line, do it as below:
int a=0, b=4, c=3;
Usage:
Important thing you would like to learn here is - you can always declare 'n' number of variables without initialization.. but if you want to use any of them, they must be initialized at least once before you use them. Using them also includes even to print them.
If you won't follow any of the above mentioned points, you must get a compilation error.
Here is the code you must follow:
public class ex{
public static void main(String[] args) {
int sum = 0 , a = 1, b = 2;
int c = 1, d = 2;
if (a < b) {
sum = sum + 1;
}
if (c < b) {
sum = sum + 1;
}
System.out.println("output :"+sum);
}
}
public class TestExample {
public static void main(String args[]){
int sum = 0 ;
int a = 1;
int b = 2;
int c = 1;
int d = 2;
if (a<b) {
sum = sum+1;
}
if (c<b) {
sum = sum+1;
}
System.out.println("output :"+sum);
}
}
declaration of variable is wrong you should not declare your variable like int a,int b= 10
avoid declaration of variable on same line.
your code gives compilation error try this one it will give output as your expectation
Don't declare variables on the same line like this, even when it's compilable. It compacts your code in a way that makes it difficult to understand, especially when you name them a,b,c and d.
int sum = 0;
int a = 1;
int b = 2;
int c = 1;
int d = 2;
Change your declaration of variables to that and the rest of the code will run fine. But I would recommend reading some basic Java tutorials so you understand how to write code that compiles. I would also suggest using an IDE so these kinds of errors are flagged while you write your code.
IDEOne (with compilation errors): http://ideone.com/rYzIf5
IDEOne (without compilation errors): http://ideone.com/rYzIf5
Try this:
int sum = 0,a = 1,b = 2;
int c = 1, d = 2;
if (a<b) {
sum++;
}
if (c<b) {
sum++;
}
System.out.println("output :"+sum);
Related
This is the task:
Implement a static-public method named "createDouble" in the class "Functionality.java". The method gets two integer values a and b as input and should transform them to a double value and return it as follows:
The first input value a, should be placed before the comma or dot.
The second input value b, should be after the comma or dot and superfluous zeros should be removed.
No imports may be used to solve this task. Also the use of the Math library or other libraries is prohibited. Implement an algorithm that contains at least one meaningful loop.
This was my idea:
public class Functionality {
public static double createDouble(int a, int b) {
double c = b;
double d = 1;
while (c >= 1) {
c /= 10;
d *= 10;
}
return a + b/d;
}
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(createDouble(12, Integer.MAX_VALUE));
}
}
The problem is I am using the Method Integer.MAX Value which I shouldn´t use. Is there another option to write this code ?
Your code looks sound, just a small tweek I would make. Your variable c will equal what you want b to be after it's done dividing so you can just use it directly. Otherwise, you don't really need to be using Integer.MAX_VALUE at all. Just use arbitrary values.
public class Functionality
{
public static double createDouble(int a, int b) {
double c = b;
while(c >= 1)
c /= 10;
return a + c;
}
public static void main(String[] args) {
System.out.println(createDouble(15, 351));
System.out.println(createDouble(32, 8452));
}
}
Output:
15.351
32.8452
Here my Implementation
public class Functionality {
private static double logb10(double num){
return (num > 1) ? 1 + logb10(num / 10) : 0;
}
public static double createOtherDouble(int a, int b) {
double c = a;
int len =(int) logb10(b);
double d = b;
for(int i = 0; i < len; i++){
d /= 10;
}
return c + d;
}
public static void main(String []args){
System.out.println(Integer.MAX_VALUE);
System.out.println(createOtherDouble(12, Integer.MAX_VALUE));
}
}
I want to get some values from one function inside of different class and use it on Main class. But it seems like I am doing somethings wrong.
public class test {
public static int enkucukbul ( double[] x){ // this method finds the smallest index
return IntStream.range(0, x.length)
.mapToObj(i -> i)
.min(Comparator.comparing(i -> x[i]))
.orElse(Integer.MIN_VALUE);
}
public static double tabu(double x, int isayi) {
Random rrandom = new Random();
float r;
double[] fxdizi = new double[4];
double[] xdizi = new double[4];
double[] hareket = new double[4];
for (int j = 0; j < isayi; j++) {
r = rrandom.nextFloat();
hareket[0] = x + 2 * r;
hareket[1] = x + 4 * r;
hareket[2] = x - 2 * r;
hareket[3] = x - 4 * r;
xdizi[0] = hareket[0];
xdizi[1] = hareket[1];
xdizi[2] = hareket[2];
xdizi[3] = hareket[3];
for (int i = 0; i < 4; i++) {
if (xdizi[i] <= 1) {
fxdizi[i] = xdizi[i] * xdizi[i];
} else {
fxdizi[i] = Math.pow((xdizi[i] - 3), 2) - 3;
}
} // for dongusu
int minIndex = enkucukbul(fxdizi); // found the smallest index
return x;
return minIndex;
return j;
return xdizi[minIndex];
return fxdizi[minIndex];
x = xdizi[minIndex]; // we found the smallest x
} // all things
}
}
Also my Main class:
public class Main {
public static void main(String[] args) {
test ts = new test();
System.out.println(ts.tabu(7.26,2));
}
}
I just want to get the values in return statements like x, j ...etc. But I get error "java:unreachable statement" on every return statement and also "java:missing return statement" in the end. Where do i do wrong?
Extra Note: I'm sorry for the localized variable names since this is my optimization class assignment
You've wrote multiple return statements in following lines :
return x;
return minIndex;
return j;
return xdizi[minIndex];
return fxdizi[minIndex];
It is not acceptable in java.
Also you are missing return value at the end of method tabuoutside of for loop. I see several logical mistakes in your code.
You need to refactor your code, also if you can explain what you are trying to achieve it will be much more clear.
In java a method can have only one return statement. You can't have more than one return statement per method unless used in an if-else block.
As the compiler tells you the remaining statements are unreachable after the first return statement.
As per your latest comment, you can create a wrapper class to hold multiple values that you want to return. For eg :
class Calculation {
public double a;
public double b;
// getters and setters
}
Now from your method you could capture the values of the variables and then store them in an object of the class created above :
public static Calculation tabu(double x, int y) {
Calculation cal = new Calculation();
// do something with x and y and other things
cal.setA(x);
cal.setB(y);
return cal;
}
I have to create a program which adds two integers and prints the sum vertically.
For example, I have.
a=323, b=322.
The output should be:
6
4
5
I've created the code for when the integers are up to two digits, but I want it to work for at least three digits.
Below is the best I could think of.
It may be completely wrong, but the only problem I'm facing is the declaration of array.
It says that the array might not be initialized.
If I set it to null then also it won't assign values to it later.
I know maybe I'm making a big mistake here, but I'll really appreciate if anyone could help me out.
Please keep in mind that I must not use any other functions for this code.
Hope I'm clear.
public class Vert
{
public static void main(String args[])
{
int n,i=0,j,a=323,b=322;
int s[];
n=a+b;
while(n>9)
{
s[i]=n%10;
i++;
s[i]=n/10;
if(s[i]>9)
{
n=s[i];
}
}
j=i;
for(j=i;j>=0;j--)
{
System.out.println(+s[j]);
}
}
}
String conversion seems like cheating, so here's a Stack.
int a = 323, b = 322;
java.util.Stack<Integer> stack = new java.util.Stack<>();
int n = a + b;
while (n > 0) {
stack.push(n % 10);
n = n / 10;
}
while (!stack.isEmpty())
System.out.println(stack.pop());
If an array is required, you need two passes over the sum
int a = 323, b = 322;
// Get the size of the array
int n = a + b;
int size = 0;
while (n > 0) {
size++;
n = n / 10;
}
// Build the output
int s[] = new int[size];
n = a + b;
for (int i = size - 1; n > 0; i--) {
s[i] = n % 10;
n = n / 10;
}
// Print
for (int x : s) {
System.out.println(x);
}
To initialize an array, you need to specify the size of your array as next:
int s[] = new int[mySize];
If you don't know the size of your array, you should consider using a List of Integer instead as next:
List<Integer> s = new ArrayList<Integer>();
Here is how it could be done:
// Convert the sum into a String
String result = String.valueOf(a + b);
for (int i=0; i <result.length();i++) {
// Print one character corresponding to a digit here per line
System.out.println(result.charAt(i));
}
I'd do it like this:
int a = 322;
int b = 322;
int sum = a + b;
String s = Integer.toString(sum);
for(int i = 0; i < s.length(); i++) {
System.out.println(s.charAt(i));
}
But your problem looks like an array is required.
The steps are same as in my solution:
Use int values
Sum the int values (operation)
Convert the int value in an array/string
Output the array/string
I developed my codes for fatorial in Java, compiling and running in a terminal with BlueJ. I corrected the error, but the fatorial doesn't give the right result. Check the code:
public class Fatorial
{
public static void main (String [] args)
{
int i, fat = 4;
for (i=1;i<fat;i++)
{
fat=fat*i;
}
System.out.print(fat);
}
}
The right reuslt should be fator(4) = 24.
It should be similar to like:
public class Fatorial3 {
public static void main(String[] args) {
int num, fatorial, aux;
num = 4;
aux = num;
fatorial = 1;
while(aux > 1){
fatorial = fatorial * (aux);// Aqui não podemos subtrair 1, porque assim seria o fatorial de num-1 (4).
aux--;
}
System.out.println("O fatorial de "+num+" é: "+fatorial);
}
}
But I want to keep my original codes.
If you unroll the loop structure, this is what you are doing:
int i, fat = 4; // fat is 4
i = 1; // i is 1
fat = fat * i; // fat = 4 * 1 = 4
++i; // i is 2
fat = fat * i; // fat = 4 * 2 = 8
++i; // i is 3
fat = fat * i; // fat = 8 * 3 = 24
++i; // i is 4, and that is less than 24, so keep going...
Eventually, fat overflows, and becomes negative, so the loop will terminate, and fat will be a large negative number.
If you want to keep a similar structure, add another variable to represent the argument of the factorial operator:
int i, n = 4, fat = 1;
for (i=1;i<=n;i++)
{
fat=fat*i;
}
System.out.print(fat);
A non-recursive solution would be to iterate down instead of up:
public static void main(String[] args) {
System.out.print(factorial(4));
}
public static long factorial(long num) {
for (long i = num-1; i > 0; i--)
{
num=num*i;
}
return num;
}
You could use a do...while loop. :)
public static int factorial(int number){
int result= 1;
int currentNumber = number;
do{
result = result * currentNumber;
currentNumber--;
}while (currentNumber > 1);
return result;
}
Then you would just print the returned result...
System.out.println(factorial(4));
You mean factorial? You can do it this way and it's far more easier using recursion
public class Factorial {
public static void main(String[] args) {
System.out.println(fact(4));
}
public static long fact (long n) {
if (n <= 1)
return 1;
else
return n * fact (n-1);
}
}
By the way when you're using for loop, you don't have to declare "i" outside the loop. Declaration is in the loop for (int i = 0; i < fat; i++)
You're reassigning i to be 4 when you declare your for loop. The reason the stated answer uses three variables for the loop is so that we don't multiply by 0 or any negative number.
Another suggestion for me would be to never declare multiple variables on the same line: It makes ones code hard to read and understand at a glance. In this case, both i and fat are being assigned 4 but this is not immediately obvious.
Another problem is that you are checking against fat, which changes with every execution of the loop. You need to be checking against a number which does not change, but fat does.
Try splitting up your variable declaration and reposting what you intend on working, that may help us guide you to where you want to be.
I tried to take input by stdin and I want "t" times the value of a, b and n. But I am getting compile error the variable might not have been initialized for the variables a, b and n.
I am not able to figure out where I went wrong.
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
int num;
Scanner in = new Scanner(System.in);
num = in.nextInt();
int sol;
sol= takken(num);
System.out.println(sol);
}
public static int takken(int howManyTimes){
int a, b, n;
int x;
int solution = 0;
Scanner d = new Scanner(System.in);
int y = 4;
for(int j = 0; j< y; j++)
{
a = d.nextInt();
b = d.nextInt();
n = d.nextInt();
}
solution = a;
int temp = 0;
for ( int i = 0; i < n; i++ ){
x = (int) Math.pow(2,i);
temp = x * b;
solution = solution + temp;
}
return solution;
}
}
Just change your declaration of a, b and n to:
int a = 0, b = 0, n = 0;
The Java compiler cannot tell that you have initialized these variables in the for-loop because you are going through the for-loop a variable number of times (that number being the variable y).
Although you declared y to have the value 4 just above the for-loop, and we can see that this means that your variables will always get a value, this is not seen by the Java compiler.
The Java compiler follows a number of strict rules when checking whether something is certain to have been initialized; and if you initialize them in a loop that is iterated over a variable number of times, even if the variable is set before, Java still doesn't see that.