lundi 19 mai 2014

Java - compter les nombres inférieurs à 10 et imprimer chacun - Stack Overflow


Code below.


I want to enter some numbers, test each one to see if it's below 10, print out the total amount below 10 and print out each number.


How do I extract the number from the loop and print it when the loop has ended?


If i enter 4,2,11,12 and the loop is finished, how do I print out the 4 and 2?


Without using arrays. I'm new to programming and have not reached arrays yet.


Thank you


class Numbers
{
public static boolean isNum(int num)
{
boolean returnValue;

returnValue = false;

if(num >= 0 && num < 10)
{
returnValue = true;
}
return returnValue;
}

public static void main (String [] args)
{
int input; //numbers entered by user
int numCount = 0; //numbers less than 10
int index;
boolean result;

System.out.print("How many numbers to test? ");
input = EasyIn.getInt();
result = isNum(input);

for(index = 0; index <= input; index++ )
{
System.out.println("Enter a number ");
input = EasyIn.getInt();
if(result == true)
{
numCount++;
}
}

System.out.println("Total numbers below 10 is " + numCount);

}
}



If you absolutely have to wait until the loop is finished to print out your numbers, and are unable to use arrays, you could always create a string and keep appending your valid numbers to it (maybe comma separated). Then just print out this String after your loop is done.


For example:


String output = "";
for (index = 0; index <= input; index++)
input = EasyIn.getInt();
result = isNum(input);

if(result == true)
{
numCount++;
output += input.ToString() + ", "; // you will probably want to remove the last comma
}
}

output = output.replaceAll(", $", ""); // remove last comma
System.out.println("Total numbers below 10 is " + numCount);
System.out.println("The numbers below 10: " + output)



Since you cannot use arrays, and assuming that other collections are out of the question as well, your only option is to print the numbers under ten as you get them. You also need to change the code to call result = isNum(input); inside the loop for each input, not just for the first number that you get:


input = EasyIn.getInt();
result = isNum(input);
if(result) // == true is implied
{
numCount++;
System.out.println("Number under ten: "+input);
}

A note on the coding style: do not compare boolean with true or false. Another note: it is OK to return results of Boolean expressions without assigning them to variables or if statements. This whole block of code


boolean returnValue;
returnValue = false;
if(num >= 0 && num < 10)
{
returnValue = true;
}
return returnValue;

is equivalent to


return num >= 0 && num < 10;


Code below.


I want to enter some numbers, test each one to see if it's below 10, print out the total amount below 10 and print out each number.


How do I extract the number from the loop and print it when the loop has ended?


If i enter 4,2,11,12 and the loop is finished, how do I print out the 4 and 2?


Without using arrays. I'm new to programming and have not reached arrays yet.


Thank you


class Numbers
{
public static boolean isNum(int num)
{
boolean returnValue;

returnValue = false;

if(num >= 0 && num < 10)
{
returnValue = true;
}
return returnValue;
}

public static void main (String [] args)
{
int input; //numbers entered by user
int numCount = 0; //numbers less than 10
int index;
boolean result;

System.out.print("How many numbers to test? ");
input = EasyIn.getInt();
result = isNum(input);

for(index = 0; index <= input; index++ )
{
System.out.println("Enter a number ");
input = EasyIn.getInt();
if(result == true)
{
numCount++;
}
}

System.out.println("Total numbers below 10 is " + numCount);

}
}


If you absolutely have to wait until the loop is finished to print out your numbers, and are unable to use arrays, you could always create a string and keep appending your valid numbers to it (maybe comma separated). Then just print out this String after your loop is done.


For example:


String output = "";
for (index = 0; index <= input; index++)
input = EasyIn.getInt();
result = isNum(input);

if(result == true)
{
numCount++;
output += input.ToString() + ", "; // you will probably want to remove the last comma
}
}

output = output.replaceAll(", $", ""); // remove last comma
System.out.println("Total numbers below 10 is " + numCount);
System.out.println("The numbers below 10: " + output)


Since you cannot use arrays, and assuming that other collections are out of the question as well, your only option is to print the numbers under ten as you get them. You also need to change the code to call result = isNum(input); inside the loop for each input, not just for the first number that you get:


input = EasyIn.getInt();
result = isNum(input);
if(result) // == true is implied
{
numCount++;
System.out.println("Number under ten: "+input);
}

A note on the coding style: do not compare boolean with true or false. Another note: it is OK to return results of Boolean expressions without assigning them to variables or if statements. This whole block of code


boolean returnValue;
returnValue = false;
if(num >= 0 && num < 10)
{
returnValue = true;
}
return returnValue;

is equivalent to


return num >= 0 && num < 10;

0 commentaires:

Enregistrer un commentaire