-
December 1st, 2005, 12:22 PM
#1
need help with this program please
I been trying since sunday to get this program to work but I keep hitting the wall it is due today and I still dont have it working please help. The program is suppossed to use an array to store 5 numbers entered by the user. If the number is under 10 or over 100 it doesn't print. If the number is the same it doesn't print. Here is my code please help.
import java.util.Scanner;
import java.util.*;
public class Arrays{
public static void main( String args[])
{
int number;
int counter = 0;
int []list;
list=new int[5];
int i=0;
for (i=0; i < list.length;i++)
{
Scanner myInput=new Scanner( System.in );
System.out.println("Enter a number:");
list[i]= myInput.nextInt();
if (i < 10 || i > 100)
{
i=0;
}
if (i==i)
{
i=0;
}
else
{
list[i]=myInput.nextInt();
}
System.out.printf( "Your numbers are: %d\n",list[i]);
}
}
}
-
December 1st, 2005, 01:20 PM
#2
Re: need help with this program please
Please enclose your code snippets in CODE tags to preserve formatting as a courtesy to others. It improves your chances of getting a response.
Originally Posted by kayler123
import java.util.Scanner;
import java.util.*;
public class Arrays{
public static void main( String args[])
{
int number;
int counter = 0;
int []list;
list=new int[5];
int i=0;
for (i=0; i < list.length;i++)
{
Scanner myInput=new Scanner( System.in );
System.out.println("Enter a number:");
list[i]= myInput.nextInt();
if (i < 10 || i > 100)
{
i=0;
}
if (i==i)
{
i=0;
}
else
{
list[i]=myInput.nextInt();
}
System.out.printf( "Your numbers are: %d\n",list[i]);
}
}
}
You don't say what doesn't work about it. What is the specific question?
"The Chicken and Rice MRE is not a personal lubricant."
-
December 1st, 2005, 01:24 PM
#3
Re: need help with this program please
I didn't write what was wrong sorry, I'm a programming noobie. It won't print and if I move the print statment around it only allows one input. On top of that it prints any number you put in and doesn't go to my if statements. I don't really know what I did wrong though.
-
December 13th, 2005, 06:57 AM
#4
Re: need help with this program please
Hi
Code:
import java.util.*;
import java.io.*;
public class Arrays
{
public static void main( String args[]) throws Exception
{
int[] list=new int[5];
boolean found = false;
System.out.println("Enter the numbers:");
for (int i=0; i < list.length;i++)
{
Scanner myInput=new Scanner( System.in );
list[i]= myInput.nextInt();
}//for
System.out.println("Your numbers are:");
for (int i=0;i < list.length;i++)
{
// check whether the no is already existing, if exists, set found to true
for(int j=0;j<list.length;j++)
{
found=false;
if( j==i ) //take care to c that not to check with the same index
continue;
if(list[i] == list[j])
{
found =true;
break;
}//if
}//for
// check the condition whether the no. <10 or >100 or is already existing
// if it is true, then dont display the no.
if ( (list[i] < 10) || (list[i] > 100) || (found == true))
continue;
System.out.println(list[i]);
} // for
}//main
}//class
This code satisfies ur request
heres the output
Enter the numbers:
9
25
32
25
117
Your numbers are:
32
-
December 14th, 2005, 07:45 AM
#5
Re: need help with this program please
Hi,
the following code satisfies ur request.it prints five numbers entered by user.It doesnot print the values below 10 and greater than 100.also if two no's are equal,it prints only once.
import java.io.*;
class parray
{
public static void main(String args[]) throws IOException
{
int a[];
a=new int[5];
System.out.println("please enter 5 numbers-no.s below ten and above
100 will not be printed-equal no's will printed only once");
for(int j=0;j<a.length;j++)
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String t=br.readLine();
int m=Integer.parseInt(t);
if((m>=10) && (m<=100))
a[j]=m;
else
a[j]=0;
}
for(int s=0;s<a.length;s++) //here the program checks if two no's are equal
{
for(int g=s+1;g<a.length;g++)
{
if(a[s]==a[g])
{
a[s]=0;
}
}}
System.out.println();
System.out.println("u entered the following numbers");
for(int j=0;j<a.length;j++)
{
if(!(a[j]==0))
System.out.println(a[j]);
}
}
}
Last edited by nuvidha; December 14th, 2005 at 10:23 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|