Generating non-repeating random numbers in Java [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to generate non-repeating random numbers. Please help me identify the problem in my code and how to fix it.
package number;
public class name {
public static void main(String[] args)
{
int counter=0;
boolean flag=true;
int number=0;
int a[] = new int[16];
try
{
while(counter<16)
{
while(flag)
{
number = (int)(Math.random()*16);
for(int i = 0; i < 16; i++)
if(a[i]==number)
{
continue;
}
else
{
System.out.println(""+i+ "===="+ number);
a[counter]=number;
flag= false;
}
}
for(int i1=0;i1<16;i1++)
{
for(int j=0;j<16;j++)
{
if(a[i1]==a[j])
{
}
else
System.out.print(" \t "+a[i1]);
}
}
}
counter --;
}
catch(Exception e)
{
e.getStackTrace();
}
}
}

You're changing the counter variable outside the while(counter<16) loop and you should increment counter counter++ instead of decrementing it.

Related

java extended class inheritance [closed]

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
Sum s = new Sum();
Sum.SetToZero z = new Sum.SetToZero();
Scanner input = new Scanner(System.in);
String read = input.nextLine();
while (!read.equals("end")) {
if (read.equals("add")) {
s.add()
}
else if (read.equals("get")) {
System.out.println(s.returnTotal());
}
else if (read.equals("zero")) {
z.zero();
}
read = input.nextLine();
}
class:
public class Sum {
int total = 0;
public void add() {
total += 1;
}
public int returnTotal() {
return total;
}
public static class SetToZero extends Sum {
public void zero() {
total = 0;
}
}
}
input:
add
add
zero
add
get
add
get
end
output:
3
4
output wanted:
1
2
Shouldn't the subclass inherit the total and set it to zero? What am I doing wrong? I know I could just move the zero into the main class but I want it to be in a separate class. thx for your help.
By making your total variable static, you can get the desired output.
class Sum {
static int total = 0;
public void add() {
total += 1;
}
public int returnTotal() {
return total;
}
public static class SetToZero extends Sum {
public void zero() {
total = 0;
}
}
}
Apart from things pointed out in names like not using lowecase letters to start your class name; I think the reason why it's not working is because you're using two different variable instances for Sum and Sum.SetToZero. You don't need to create a new variables since SetToZero has all the attributes of Sum. I think you should change this:
Sum s = new Sum();
Sum.SetToZero z = new Sum.SetToZero();
Sum.SetToZero s = new Sum.SetToZero(); // use s for all operations
Here is how your modified main method would look:
public static void main(String[] args) {
Sum.SetToZero s = new Sum.SetToZero();
Scanner input = new Scanner(System.in);
String read = input.nextLine();
while (!read.equals("end")) {
if (read.equals("add")) {
s.add();
}
else if (read.equals("get")) {
System.out.println(s.get());
}
else if (read.equals("zero")) {
s.zero();
}
read = input.nextLine();
}
}
When I ran this, I saw expected output:
src : $ java Sum
add
add
zero
add
get
1
add
get
2
end

Java: Boolean not returning false to Main method [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Here's a little code I wrote; in short, I'm plain stuck and can't figure out what Im doing wrong. Basically what my intention for the code is, is to check my boolean array; find out if true is listed more consecutive or if false is. False is of course listed more so it then should return false to my main method.
public class FalseBoolean
{
public static void main(String [] args) {
boolean[] guess = {false,true,false,false,false,true,true};
boolean result = longerTF(guess);
}
public static boolean longerTF(boolean[] guess) {
int variableTrue = 0;
int variableFalse = 0;
for(int x = 0; x < guess.length; x++) {
if(guess[x] == true) {
variableTrue++;
} else {
variableFalse++;
}
return variableFalse;
}
}
}
The immediate problem should be returning a boolean, not an integer. You can do this by simply checking which count is greatest.
Your second problem is to return after you looked at the entire array, not just the first element.
public static boolean longerTF(boolean[] guess) {
int variableTrue = 0;
int variableFalse = 0;
for(int x = 0; x < guess.length; x++) {
if(guess[x]) {
variableTrue++;
} else {
variableFalse++;
}
}
return variableTrue >= variableFalse;
}
Note: you only need one counter for an array of two possible values ... For example,
int variableFalse = guess.length - variableTrue;
Your question asked for consecutive elements, but this code only returns which occurs the most in the entire array, so keep working on the logic
Try This(Made some logic change)
public class FalseBoolean
{
public static void main(String [] args)
{
boolean[] guess =
{false,true,true,true,true,false,false,false,true,true};
boolean result = longerTF(guess);
System.out.println(result);
}
public static boolean longerTF(boolean[] guess)
{
int consecutiveVariableTrue = 0, maxConsecutiveVariableTrue = 0;
int consecutiveVariableFalse = 0, maxConsecutiveVariableFalse = 0;
for(int x = 0; x < guess.length; x++)
{
if(guess[x] == true) {
consecutiveVariableTrue++;
if (maxConsecutiveVariableTrue < consecutiveVariableTrue)
maxConsecutiveVariableTrue = consecutiveVariableTrue;
} else {
consecutiveVariableTrue = 0;
}
}
for(int x = 0; x < guess.length; x++)
{
if(guess[x] == false) {
consecutiveVariableFalse++;
if (maxConsecutiveVariableFalse < consecutiveVariableFalse)
maxConsecutiveVariableFalse = consecutiveVariableFalse;
} else {
consecutiveVariableFalse = 0;
}
}
if (maxConsecutiveVariableTrue >= maxConsecutiveVariableFalse) {
return true;
}
return false;
}
}
You are trying to return an int variable when you obviously declared your method as a boolean type. Do this instead:
if(variableFalse > variableTrue){
return false;
}else if(variableTrue > variableFalse){
return true;
}else{
//Default return statement
}
Also, in this specific case (because you are processing an array), don't include the return statement in the for loop.
I would explain this better if I wasn't using mobile :P

Smallest and Largest integers using if/else only [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Here is my code to display smallest and largest integer by taking 5 inputs from user...it works for smallest values but not for largest and I cant figure out the problem...please help
import java.util.Scanner;
public class LargestAndSmallestIntegers {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int a,b,c,d,e;
int small,large;
System.out.println("Enter five integer values...");
a=input.nextInt();
small=a;
large=a;
b=input.nextInt();
if (small<b)
{
large=b;
}
else
{
small=b;
}
c=input.nextInt();
if (small<c)
{
large=c;
}
else
{
small=c;
}
d=input.nextInt();
if (small<d)
{
large=d;
}
else
{
small=d;
}
e=input.nextInt();
if (small<e)
{
large=e;
}
else
{
small=e;
}
input.close();
System.out.printf("%d is smallest and %d is largest", small,large);
}
}
private int a = input.nextInt(),
b = input.nextInt(),
c = input.nextInt(),
d = input.nextInt(),
e = input.nextInt();
private int small, large;
small = min(a,b);
small = min(small,c);
small = min(small,d);
small = min(small,e);
large = max(a,b);
large = max(large,c);
large = max(large,d);
large = max(large,e);
private int min(int a, int b) {
if (a < b) return a else return b;
}
private int max(int a, int b) {
if (a > b) return a else return b;
}
I think this works ;)
it works for smallest values but not for largest
As lared pointed out, comparing against small to determine large is flawed. You must include something like this in your comparison logic:
if (large < b)
{
large = b;
}
in addition to your existing comparison:
if (small > b)
{
small = b;
}
you always just check for the smallest value. you also need to check
if (small<e)
{
if(large < e)
{
large=e;
}
}

How do I invoke a method with array parameter in java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an assignment in which I have to perform operations on array in Java, I have to make separate functions of each operation, which I will write but I can not figure out how to invoke a method with array parametres. I usually program in c++ but this assignment is in java. If any of you can help me, I'd be really thankful. :)
public class HelloJava {
static void inpoot() {
Scanner input = new Scanner(System.in);
int[] numbers = new int[10];
System.out.println("Please enter 10 numbers ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = input.nextInt();
}
}
static void outpoot(int[] numbers) {
for(int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
public static void main(String[] args) {
inpoot();
outpoot(numbers); //can not find the symbol
}
}
Your inpoot method has to return the int[] array, and then you pass it to outpoot as a parameter:
public class HelloJava {
static int[] inpoot() { // this method has to return int[]
Scanner input = new Scanner(System.in);
int[] numbers = new int[10];
System.out.println("Please enter 10 numbers ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = input.nextInt();
}
return numbers; // return array here
}
static void outpoot(int[] numbers) {
for(int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
public static void main(String[] args) {
int[] numbers = inpoot(); // get the returned array
outpoot(numbers); // and pass it to outpoot
}
}
When you call outpoot it should be
outpoot (numbers);

Permutation Cycle [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
This is my code. I have 1 error. Please help me.
Try this input:
8
1 2 3 4 5 6 7 8
..........................................................................................
import java.io.*;
public class permutation
{
public static void main (String []args) throws IOException
{
BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in) );
String n="";
int parsen=0, parsenum=0;
System.out.println(n);
n = dataIn.readLine();
parsen=Integer.parseInt(n);
String num[]= new String [1000];
int visited[]=new int [1000];
int vindex[]=new int [2000];
int a=0;
for(int i=1;i<=parsen;i++){
num[i-1]=dataIn.readLine();
parsenum=Integer.parseInt(num[i-1]);
}
int t_visited=0, cycles=0, start=0, index=0;
while(t_visited<parsen)
{
for(int i=1; i<=parsen;i++)
{
if(visited[i]==0)
{
vindex[start]=i;
visited[i]=1;
t_visited++;
index=start;
break;
}
}
while(true)
{
index++;
vindex[index]=parsenum[vindex[index-1]];
if(vindex[index]==vindex[start])
break;
visited[vindex[index]]=1;
t_visited++;
vindex[++index]=0;
start=index+1;
cycles++;
}
System.out.println(cycles+vindex[0]);
for(int i=0;i<(parsen+2*cycles);i++)
{
if(vindex[i]==0)
{
System.out.println();
}else{
System.out.println(vindex[i]);
}
}
}
}
}
Thank you!
Another code:
working but wrong output. help pls...i declare num as an integer.
import java.io.*;
public class permutation
{
public static void main (String []args) throws IOException
{
BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in) );
String n="";
int parsen=0;
System.out.println(n);
n = dataIn.readLine();
parsen=Integer.parseInt(n);
int num[]= new int [1000];
int visited[]=new int [1000];
int vindex[]=new int [2000];
int a=0;
for(int i=1;i<=parsen;i++){
num[i-1] = Integer.parseInt(System.console().readLine());
}
int t_visited=0, cycles=0, start=0, index=0;
while(t_visited<parsen)
{
for(int b=1; b<=parsen;b++)
{
if(visited[b]==0)
{
vindex[start]=b;
visited[b]=1;
t_visited++;
index=start;
break;
}
}
while(true)
{
index++;
vindex[index]=num[vindex[index-1]];
if(vindex[index]==vindex[start])
break;
visited[vindex[index]]=1;
t_visited++;
vindex[++index]=0;
start=index+1;
cycles++;
}
System.out.println(cycles+vindex[0]);
for(int c=0;c<(parsen+2*cycles);c++)
{
if(vindex[c]==0)
{
System.out.println();
}else{
System.out.print(vindex[c]);
}
}
}
}
}
the output must be
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
parsenum is an int - but you use it as if it were an array: parsenum[vindex[index-1]]. That can't work.
Instead of declaring int parsenum=0; you will have to declare int parsenum[10]; where 10 will be the size of your array.

Categories

Resources