Reset the value of each variable
ill start with showing my program with 2 different codes and data flow
1st
Code :
package xTestsx;
import java.io.*;
/**
*
* @author
*/
//I just have to make a control flow for the number of orders that is less than 20
public class CoffeBeans_CONT_UF {
private static BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
public static void main(String[] args) throws IOException {
int numberOfOrders;
int largeBox = 0,
mediumBox = 0,
smallBox = 0,
excess = 0;
int orderCount = 0,
firstSmallBoxCount = 0,
nextSmallBoxCount = 0,
mediumBoxCount = 0,
largeBoxCount = 0;
int totalBox = 0;
do {
do {
do {
System.out.print("Order: ");
numberOfOrders = Integer.parseInt(br.readLine());
if (numberOfOrders <= 5) {
firstSmallBoxCount = numberOfOrders;
smallBox++;
System.out.print("\n");
System.out.println("Your Order Is Too Small.");
}
else if (numberOfOrders < 10) {
firstSmallBoxCount = 5;
for (orderCount = numberOfOrders; orderCount >= 5; orderCount = orderCount - 5) {
smallBox++;
excess = orderCount - 5;
}
if ((excess <= 4) && (excess != 0)) {
nextSmallBoxCount = excess;
excess = 0;
}
System.out.print("\n");
System.out.println("Your Order Is Too Small.");
}
}
while (numberOfOrders < 10 && ReOrder());
if (numberOfOrders >= 10) {
break;
}
}
while (ContinueShipment()); //this part
if (numberOfOrders < 20) {
mediumBoxCount = 10;
for (orderCount = numberOfOrders; orderCount >= 10; orderCount = orderCount - 10) {
mediumBox++;
excess = orderCount - 10;
}
if (excess >= 5 ) {
firstSmallBoxCount = 5;
for (; excess >= 5; excess = excess - 5) {
smallBox++;
}
}
if ((excess <= 4) && (excess != 0)) {
smallBox++;
nextSmallBoxCount = excess;
excess = 0;
}
}
else if (numberOfOrders >= 20) {
largeBoxCount = 20;
for (orderCount = numberOfOrders; orderCount >= 20; orderCount = orderCount - 20) {
largeBox++ ;
excess = orderCount - 20;
}
if (excess >= 10) {
mediumBoxCount = 10;
for (; excess >= 10; excess = excess - 10) {
mediumBox++;
}
}
if (excess >= 5) {
firstSmallBoxCount = 5;
for (; excess >= 5; excess = excess - 5) {
smallBox++;
}
}
if ((excess <= 4) && (excess != 0)) {
smallBox++;
nextSmallBoxCount = excess;
excess = 0;
}
}
if (largeBox == 0) {
if ((mediumBox == 1) && (smallBox > 1)) {
System.out.print("\n");
System.out.println("Medium Box Contains " + mediumBoxCount + " Orders");
System.out.println("The First Small Box Contains " + firstSmallBoxCount + " Orders");
System.out.println("And The Remaining Small Box Contains " + nextSmallBoxCount + " Orders");
}
else if ((mediumBox == 1) & (smallBox == 1)) {
if (firstSmallBoxCount == 5) {
System.out.print("\n");
System.out.println("Medium Box Contains " + mediumBoxCount + " Orders");
System.out.println("Small Box Contains " + firstSmallBoxCount + " Orders");
}
else if (nextSmallBoxCount < 5) {
System.out.print("\n");
System.out.println("Medium Box Contains " + mediumBoxCount + " Orders");
System.out.println("Small Box Contains " + nextSmallBoxCount + " Orders");
}
}
else if ((mediumBox == 1) && (smallBox == 0)) {
System.out.print("\n");
System.out.println("Medium Box Contains " + mediumBoxCount + " Orders");
}
else if ((mediumBox == 0) && (smallBox > 1)) {
System.out.println("\n");
System.out.println("The First Small Box Contains " + firstSmallBoxCount + " Orders");
System.out.println("And The Remaining Small Box Contains " + nextSmallBoxCount + " Orders");
}
}
else if ((mediumBox == 0) && (smallBox == 0)) {
System.out.print("\n");
System.out.println(largeBox + " " + "Large Box");
if (largeBox == 1) {
System.out.print("\n");
System.out.println("Large Box Contains" + " " + largeBoxCount + " " + "Orders");
}
else if (largeBox > 1) {
System.out.print("\n");
System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
}
}
else if (mediumBox <= 0) {
System.out.print("\n");
System.out.println(smallBox + " " + "Small Box");
System.out.println(largeBox + " " + "Large Box");
if (largeBox == 1) {
if (firstSmallBoxCount < 5) {
System.out.print("\n");
System.out.println("Large Box Contains" + " " + largeBoxCount + " " + "Orders");
System.out.println("And The Small Box Contains" + " " + nextSmallBoxCount + " " + "Order");
}
else if (firstSmallBoxCount >= 5) {
System.out.print("\n");
System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
System.out.println("The First Small Contains" + " " + firstSmallBoxCount + " " + "Orders");
System.out.println("And The Reamaining Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
}
}
else if (largeBox > 1) {
if (firstSmallBoxCount >= 5) {
System.out.print("\n");
System.out.println("Each Large Box Has" + " " + largeBoxCount + " " + "Orders");
System.out.println("The First Small Box Has" + " " + firstSmallBoxCount + " " + "Orders");
System.out.println("And The Reamaining Small Box Has" + " " + nextSmallBoxCount + " " + "Orders");
}
else if (firstSmallBoxCount < 5) {
System.out.print("\n");
System.out.println("Each Large Box Has " + " " + largeBoxCount + " " + "Orders");
System.out.println("And The Small Box Has" + " " + nextSmallBoxCount + " " + "Order");
}
}
}
else if (smallBox <= 0) {
System.out.print("\n");
System.out.println(mediumBox + " " + "Medium Box");
System.out.println(largeBox + " " + "Large Box");
if (mediumBox == 1) {
if (largeBox == 1) {
System.out.print("\n");
System.out.println("One Large Box Contains " + " " + largeBoxCount + " " + "Orders");
System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
}
else if (largeBox > 1) {
System.out.print("\n");
System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
}
}
}
else if ((largeBox >= 1) && (mediumBox >= 1) && (smallBox >= 1)) {
System.out.print("\n");
System.out.println(smallBox + " " + "Small Box");
System.out.println(mediumBox + " " + "Medium Box");
System.out.println(largeBox + " " + "Large Box");
if (largeBox == 1) {
if (mediumBox == 1) {
if (smallBox > 1) {
if (firstSmallBoxCount >= 5) {
System.out.print("\n");
System.out.println("One Large Box Contains" + " " + largeBoxCount + " " + "Orders");
System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
System.out.println("The First Small Box Contains" + " " + firstSmallBoxCount + " " + "Orders");
System.out.println("The Remaining Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
}
}
else if (smallBox == 1) {
System.out.print("\n");
System.out.println("One Large Box Contains" + " " + largeBoxCount + " " + "Orders");
System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
System.out.println("One Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
}
}
}
else if (largeBox > 1) {
if (mediumBox == 1) {
if (smallBox > 1) {
if (firstSmallBoxCount >= 5) {
System.out.print("\n");
System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
System.out.println("The First Small Box Contains" + " " + firstSmallBoxCount + " " + "Orders");
System.out.println("The Remaining Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
}
}
else if (smallBox == 1) {
System.out.print("\n");
System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
System.out.println("One Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
}
}
}
}
totalBox = largeBox + mediumBox + smallBox;
if (totalBox > 1) {
System.out.print("\n");
System.out.println("The Shipment Will Carry A Total Of: " + totalBox + " " + "Boxes");
}
else if (totalBox == 1) {
System.out.print("\n");
System.out.println("The Shipment Will Carry A Total Of: " + totalBox + " " + "Box");
}
}
while (ContinueOrdering());
}
public static boolean ContinueShipment() throws IOException {
String cont;
System.out.print("\n");
System.out.print("Do You Wish To Include Your Orders To The Shipment?: ");
cont = br.readLine();
if ((cont.equalsIgnoreCase("Y")) || (cont.equalsIgnoreCase("YES"))) {
return false;
}
else if ((cont.equalsIgnoreCase("N")) || (cont.equalsIgnoreCase("NO"))) {
System.out.print("\n");
System.out.print("Come Again!");
return false;
}
else {
return false;
}
}
public static boolean ReOrder() throws IOException {
String reOrder;
System.out.print("\n");
System.out.println("And Your Orders Might Scramble With Other Orders");
System.out.print("Do You Want To Order Again?: ");
reOrder = br.readLine();
if ((reOrder.equalsIgnoreCase("Y")) || (reOrder.equalsIgnoreCase("YES"))) {
System.out.print("\n");
return true;
}
else if ((reOrder.equalsIgnoreCase("N")) || (reOrder.equalsIgnoreCase("NO"))) {
return false;
}
else {
return false;
}
}
public static boolean ContinueOrdering() throws IOException {
String cont;
System.out.print("\n");
System.out.print("Continue Ordering? :");
cont = br.readLine();
if ((cont.equalsIgnoreCase("Y")) || (cont.equalsIgnoreCase("YES"))) {
System.out.print("\n");
return true;
}
else if ((cont.equalsIgnoreCase("N")) || (cont.equalsIgnoreCase("NO"))) {
return false;
}
else {
return false;
}
}
}
2nd
Code :
package xTestsx;
import java.io.*;
/**
*
* @author
*/
//I just have to make a control flow for the number of orders that is less than 20
public class CoffeBeans2_CONT_UF {
private static int numberOfOrders;
private static int largeBox = 0,
mediumBox = 0,
smallBox = 0,
excess = 0;
private static int orderCount = 0,
firstSmallBoxCount = 0,
nextSmallBoxCount = 0,
mediumBoxCount = 0,
largeBoxCount = 0;
private static int totalBox = 0;
private static BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
public static void main(String[] args) throws IOException {
do {
do {
do {
System.out.print("Order: ");
numberOfOrders = Integer.parseInt(br.readLine());
if (numberOfOrders <= 5) {
firstSmallBoxCount = numberOfOrders;
smallBox++;
System.out.print("\n");
System.out.println("Your Order Is Too Small.");
}
else if (numberOfOrders < 10) {
firstSmallBoxCount = 5;
for (orderCount = numberOfOrders; orderCount >= 5; orderCount = orderCount - 5) {
smallBox++;
excess = orderCount - 5;
}
if ((excess <= 4) && (excess != 0)) {
nextSmallBoxCount = excess;
excess = 0;
}
System.out.print("\n");
System.out.println("Your Order Is Too Small.");
}
}
while (numberOfOrders < 10 && ReOrder());
if (numberOfOrders >= 10) {
break;
}
}
while (ContinueShipment()); //this part
if (numberOfOrders < 20) {
mediumBoxCount = 10;
for (orderCount = numberOfOrders; orderCount >= 10; orderCount = orderCount - 10) {
mediumBox++;
excess = orderCount - 10;
}
if (excess >= 5 ) {
firstSmallBoxCount = 5;
for (; excess >= 5; excess = excess - 5) {
smallBox++;
}
}
if ((excess <= 4) && (excess != 0)) {
smallBox++;
nextSmallBoxCount = excess;
excess = 0;
}
}
else if (numberOfOrders >= 20) {
largeBoxCount = 20;
for (orderCount = numberOfOrders; orderCount >= 20; orderCount = orderCount - 20) {
largeBox++ ;
excess = orderCount - 20;
}
if (excess >= 10) {
mediumBoxCount = 10;
for (; excess >= 10; excess = excess - 10) {
mediumBox++;
}
}
if (excess >= 5) {
firstSmallBoxCount = 5;
for (; excess >= 5; excess = excess - 5) {
smallBox++;
}
}
if ((excess <= 4) && (excess != 0)) {
smallBox++;
nextSmallBoxCount = excess;
excess = 0;
}
}
if (largeBox == 0) {
if ((mediumBox == 1) && (smallBox > 1)) {
System.out.print("\n");
System.out.println("Medium Box Contains " + mediumBoxCount + " Orders");
System.out.println("The First Small Box Contains " + firstSmallBoxCount + " Orders");
System.out.println("And The Remaining Small Box Contains " + nextSmallBoxCount + " Orders");
}
else if ((mediumBox == 1) & (smallBox == 1)) {
if (firstSmallBoxCount == 5) {
System.out.print("\n");
System.out.println("Medium Box Contains " + mediumBoxCount + " Orders");
System.out.println("Small Box Contains " + firstSmallBoxCount + " Orders");
}
else if (nextSmallBoxCount < 5) {
System.out.print("\n");
System.out.println("Medium Box Contains " + mediumBoxCount + " Orders");
System.out.println("Small Box Contains " + nextSmallBoxCount + " Orders");
}
}
else if ((mediumBox == 1) && (smallBox == 0)) {
System.out.print("\n");
System.out.println("Medium Box Contains " + mediumBoxCount + " Orders");
}
else if ((mediumBox == 0) && (smallBox > 1)) {
System.out.println("\n");
System.out.println("The First Small Box Contains " + firstSmallBoxCount + " Orders");
System.out.println("And The Remaining Small Box Contains " + nextSmallBoxCount + " Orders");
}
}
//END
else if ((mediumBox == 0) && (smallBox == 0)) {
System.out.print("\n");
System.out.println(largeBox + " " + "Large Box");
if (largeBox == 1) {
System.out.print("\n");
System.out.println("Large Box Contains" + " " + largeBoxCount + " " + "Orders");
}
else if (largeBox > 1) {
System.out.print("\n");
System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
}
}
else if (mediumBox <= 0) {
System.out.print("\n");
System.out.println(smallBox + " " + "Small Box");
System.out.println(largeBox + " " + "Large Box");
if (largeBox == 1) {
if (firstSmallBoxCount < 5) {
System.out.print("\n");
System.out.println("Large Box Contains" + " " + largeBoxCount + " " + "Orders");
System.out.println("And The Small Box Contains" + " " + nextSmallBoxCount + " " + "Order");
}
else if (firstSmallBoxCount >= 5) {
System.out.print("\n");
System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
System.out.println("The First Small Contains" + " " + firstSmallBoxCount + " " + "Orders");
System.out.println("And The Reamaining Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
}
}
else if (largeBox > 1) {
if (firstSmallBoxCount >= 5) {
System.out.print("\n");
System.out.println("Each Large Box Has" + " " + largeBoxCount + " " + "Orders");
System.out.println("The First Small Box Has" + " " + firstSmallBoxCount + " " + "Orders");
System.out.println("And The Reamaining Small Box Has" + " " + nextSmallBoxCount + " " + "Orders");
}
else if (firstSmallBoxCount < 5) {
System.out.print("\n");
System.out.println("Each Large Box Has " + " " + largeBoxCount + " " + "Orders");
System.out.println("And The Small Box Has" + " " + nextSmallBoxCount + " " + "Order");
}
}
}
else if (smallBox <= 0) {
System.out.print("\n");
System.out.println(mediumBox + " " + "Medium Box");
System.out.println(largeBox + " " + "Large Box");
if (mediumBox == 1) {
if (largeBox == 1) {
System.out.print("\n");
System.out.println("One Large Box Contains " + " " + largeBoxCount + " " + "Orders");
System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
}
else if (largeBox > 1) {
System.out.print("\n");
System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
}
}
}
else if ((largeBox >= 1) && (mediumBox >= 1) && (smallBox >= 1)) {
System.out.print("\n");
System.out.println(smallBox + " " + "Small Box");
System.out.println(mediumBox + " " + "Medium Box");
System.out.println(largeBox + " " + "Large Box");
if (largeBox == 1) {
if (mediumBox == 1) {
if (smallBox > 1) {
if (firstSmallBoxCount >= 5) {
System.out.print("\n");
System.out.println("One Large Box Contains" + " " + largeBoxCount + " " + "Orders");
System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
System.out.println("The First Small Box Contains" + " " + firstSmallBoxCount + " " + "Orders");
System.out.println("The Remaining Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
}
}
else if (smallBox == 1) {
System.out.print("\n");
System.out.println("One Large Box Contains" + " " + largeBoxCount + " " + "Orders");
System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
System.out.println("One Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
}
}
}
else if (largeBox > 1) {
if (mediumBox == 1) {
if (smallBox > 1) {
if (firstSmallBoxCount >= 5) {
System.out.print("\n");
System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
System.out.println("The First Small Box Contains" + " " + firstSmallBoxCount + " " + "Orders");
System.out.println("The Remaining Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
}
}
else if (smallBox == 1) {
System.out.print("\n");
System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
System.out.println("One Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
}
}
}
}
totalBox = largeBox + mediumBox + smallBox;
if (totalBox > 1) {
System.out.print("\n");
System.out.println("The Shipment Will Carry A Total Of: " + totalBox + " " + "Boxes");
}
else if (totalBox == 1) {
System.out.print("\n");
System.out.println("The Shipment Will Carry A Total Of: " + totalBox + " " + "Box");
}
}
while (ContinueOrdering());
}
public static boolean ContinueShipment() throws IOException {
String cont;
System.out.print("\n");
System.out.print("Do You Wish To Include Your Orders To The Shipment?: ");
cont = br.readLine();
if ((cont.equalsIgnoreCase("Y")) || (cont.equalsIgnoreCase("YES"))) {
return false;
}
else if ((cont.equalsIgnoreCase("N")) || (cont.equalsIgnoreCase("NO"))) {
largeBox = 0;
mediumBox = 0;
smallBox = 0;
excess = 0;
numberOfOrders = 0;
orderCount = 0;
firstSmallBoxCount = 0;
nextSmallBoxCount = 0;
mediumBoxCount = 0;
largeBoxCount = 0;
return false;
}
else {
return false;
}
}
public static boolean ReOrder() throws IOException {
String reOrder;
System.out.print("\n");
System.out.println("And Your Orders Might Scramble With Other Orders");
System.out.print("Do You Want To Order Again?: ");
reOrder = br.readLine();
if ((reOrder.equalsIgnoreCase("Y")) || (reOrder.equalsIgnoreCase("YES"))) {
System.out.print("\n");
return true;
}
else if ((reOrder.equalsIgnoreCase("N")) || (reOrder.equalsIgnoreCase("NO"))) {
return false;
}
else {
return false;
}
}
public static boolean ContinueOrdering() throws IOException {
String cont;
System.out.print("\n");
System.out.print("Continue Ordering? :");
cont = br.readLine();
if ((cont.equalsIgnoreCase("Y")) || (cont.equalsIgnoreCase("YES"))) {
System.out.print("\n");
return true;
}
else if ((cont.equalsIgnoreCase("N")) || (cont.equalsIgnoreCase("NO"))) {
return false;
}
else {
return false;
}
}
}
my logic is this.... in the part of the method ContinueOrdering()..(each code) it will ask me if i want to include the orders in the shipment... i having a hard problem with the first code so what i did is the second code..
if i enter "n" or "no" it will RESET all The VALUES of the DATA MEMBERS(field),,
or lets just say that i the method ask me
Code :
If I Want To Include THe Orders In the Shipment
and i answered no.. then it will not ADD.. (simply by reseting the value of each FIELD)..
problem:
1.) regarding with static fields , those fields as you can see is ineffient because its not a constant nor a class datamember,
2.) i want to resolve this by using the first code,, (using local variables instead of datamembers/fields)
3.) is there any method in java that can reset a value of a variable? (field or local)
4.) or one of the effient way to do this is to define a class for this program?
Re: Reset the value of each variable
This code will loop until your continueOrdering method returns false. If you have any questions, please ask.
Code :
// initialize your variables
int one = 0;
int two = 0;
while (true) {
// do your logic
if (continueOrdering()) {
// reset your variables
}
else {
break;
}
}
Re: Reset the value of each variable
I'd say if possible avoid using the while(true), for(;;) or any other type of infinite loop definition. It makes reading the code very hard and you need to be really sure about your code inside the block that it will eventually break out of it.
You might possibly run into compiler errors with unreachable code if you use these types of blocks.
// Json
Re: Reset the value of each variable
Quote:
Originally Posted by
Json
I'd say if possible avoid using the while(true), for(;;) or any other type of infinite loop definition. It makes reading the code very hard and you need to be really sure about your code inside the block that it will eventually break out of it.
You might possibly run into compiler errors with unreachable code if you use these types of blocks.
// Json
Isn't that the job of the programmer to begin with?
For production code, I would say you're right if there is a case the loop might never stop. If you're just writing a toy program, then there is no harm.
Re: Reset the value of each variable
the suggestion for this is? :confused:
Re: Reset the value of each variable
Quote:
Originally Posted by
chronoz13
the suggestion for this is? :confused:
Did you see my first post?
To make Json happy, here's a version without an infinite loop. ;-) It is still just as "unsafe" as an infinite loop, but with different constructs.
What I am offering you is only a template. You still need to add your logic.
Code :
// initialize your variables
int one = 0;
int two = 0;
boolean run = true;
while (run) {
// do your logic
if (continueOrdering()) {
// reset your variables
}
else {
run = false;
}
}