Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here, try this:</p> <p>I think it pretty much does what you need.</p> <pre><code>package hw; import java.util.Scanner; public class Problem1 { public static void main (String [] args) { int cost; int number; double cost_unit = 0; double total = 0; Scanner entrada = new Scanner(System.in); System.out.println("Please enter the cost of the product."); cost = entrada.nextInt(); System.out.println("Please enter the amount of units to be sold"); number = entrada.nextInt(); cost_unit = cost * 1.4; if (number&gt;0) { total = cost_unit*number; } System.out.printf("Cost per unit will be $ %f\n",cost_unit); System.out.printf("Total cost will be $ %f\n",total); } } </code></pre> <p>Try it out and see if it works. By any chance are you at the ITAM?</p> <p><strong>EDIT</strong></p> <p>About the loop your original was correct. Just have to surround the code you want to repeat, and add a condition to exit.</p> <p>Something like this after creating the scanner ( pretty much the way you did in the first attempt ) :</p> <pre><code> while( cost &gt; 0 ) { System.out.println("Please enter the cost of the product ( 0 to exit the progam"); cost = entrada.nextInt(); ......... ......... ......... System.out.printf("Total cost will be $ %f\n",total) } </code></pre> <p>That will repeat the code between braces while cost is greater than 0.</p> <p>Of course you should change the initial value of cost, otherwise it won't enter in the lopp the first time and perhaps you should clean up the values before each iteration.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload