This question already has answers here:
Does Java have something like C#'s ref and out keywords?
(7 answers)
How to use an output parameter in Java? [duplicate]
(7 answers)
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
import java.util.*;
public class Main {
private static int call(int n) {
n = n - 1;
return n;
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = 5;
call(n);
System.out.println(n);//output is still 5;
}
}
how to change the value of n using call method?
I did using void on call method like we use for integer array but that is also not working.
Related
This question already has answers here:
Get random boolean in Java
(11 answers)
Closed 3 years ago.
I'm trying to do a tamagotchi ( a virtual pet wich you take care of) on Java, and i want to do by using a boolean random a way to show it randomly if the tamagotchi is sick or not but i don't know how to do it, i have 3 classes the cat the animal and the application.
You could use this line:
new Random().nextInt(1);
This line returns an Integer: 0 (false) or 1 (true).
Try this:
import java.util.Random;
public class Tamagotchi {
static boolean isSick;
public static void main(String[] args){
isSick = new Random().nextBoolean();
System.out.println("Pet is sick: " + isSick);
}
}
This question already has answers here:
commands in java to clear the screen
(11 answers)
Closed 5 years ago.
I'm writing a text-based unit converter and I want to be able to run a clear command so that the window that the program I want to know how I can do it.
I think you can System.exec("clear") but that depends on what operating system the program is running on.
This is my choice for clear:
public static void clearConsole() {
String value = "\n\r";
for (int i = 0; i < 5; ++i) {
value = value + value;
System.out.printf(value);
}
}
This question already has answers here:
How do the post increment (i++) and pre increment (++i) operators work in Java?
(14 answers)
Closed 6 years ago.
I was trying to reverse String using recursion as follows.
But when I am calling reverse(arr,start++,end--) it is giving stackoverflow error.
I tried using reverse(arr,start+1,end-1) then it is working fine.
public class Reverse {
public static void main(String[] args) {
char inp[] = "TestString".toCharArray();
int n = inp.length;
reverse(inp, 0,n-1);
System.out.println(String.valueOf(inp));
}
public static void reverse(char[] arr, int start, int end){
if(start>=end)
return ;
char tmp;
tmp = arr[start];
arr[start] = arr[end];
arr[end] = tmp;
reverse(arr,start+1,end-1);//error line
}
What is the problem with reverse(arr,start++,end--)?
I want to know why value of start++ and end-- will not get passed to function.
Have a look at this SO question
SO post-increment-and-pre-increment-concept
Post
reverse(arr,start++,end--) when you do this the incremented/decremented value will not be passed to recursive method, means you are calling the method with original value of start and end result in SO error
Pre
When you do reverse(arr,start+1,end-1) or reverse(arr,++start,--end) incremented and decremented value of start and end will be passed to recursive method.
While debugging in Eclipse IDE check the values of start and end in variables panel if not using IDE write start and end values to console in reverse method
This question already has answers here:
Possible lossy conversion from double to int
(3 answers)
Closed 6 years ago.
I keep getting an error but Im not understanding why. The error is about not being able to convert from double to int but I have everything declared in double so I dont understand why im getting this error.
class ConeArray{
public static void main(String[] args) {
double[] coneArray;
for(double i = 0; i < coneArray.length; i++) {
coneArray = Math.PI * Math.pow((i*2),2) * (1/3 * (4 * i));
System.out.println("Volume of cone: " + coneArray[i]);
}
}
}
You have declared double i in your for loop. Array indexes must be ints.
Also, I don't see you actually instantiating the array in the code sample you provided. The current implementation will throw a NPE if you try to access it.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
My Variable c is always zero. I dont understand why its not updating. can anyone please explain why this is happening. what should i do to avoid this
public static int linearSearch(Exam[] marks, String name) {
int c =0;
if( marks==null)
{
return -1;
}
else{
for(int i=0;i<marks.length;i++)
{
//System.out.println(a[i]);
if(performances[i].getName()==name)
{
c= i;
}
}
}
return c;
//to be completed
}
Modify this line as below
performances[i].getName().equalsIgnoreCase(name)
if you want to Ignore upper or lower case
else use the below
performances[i].getName().equals(name)
to check the content of the name instead of references.