java program correction

-1

I'm new to programming with Java , and I do not know what similarities I have with C , if someone can correct me the program I would appreciate it. The objective of the program is to calculate a supermarket bill. When I give run only the end of the program comes out with the message "the value of your invoice is: 0.0".

Whoever can answer my question please tell me where my error or errors are.

package punto.pkg3;

import java.util.Scanner;

public class Punto3 {

public static void main(String[] args) {

    Scanner entrada=new Scanner (System.in);

    double a=0;

    double b=0;

    double c=0;

    double d=0;

    double e=0;

    double f=0;

    double g=0;

    while (a==1) {

    System.out.println("hay mas productos?");

        System.out.println("1=si, 2 =no");

         a=entrada.nextDouble();

        while (a!=1 | a!=2){

            System.out.println("no es un valor valido, vuelva a digitar");

         a=entrada.nextDouble();

        }

        System.out.println("digite el valor del producto");

        b=entrada.nextDouble();

        System.out.println("cuantas unidades llevo de este producto?");

        c=entrada.nextDouble();

        d=b*c;

        e=d+(d*16/100);

        if(e>=50000)

            e=e-(e*5/100);

        else

            break;

      f=f+e;

    }

    System.out.println("el valor de su factura es:" +f);
}

}
    
asked by sebastian guzman 02.09.2018 в 02:25
source

3 answers

0

The while you use for a = 1, and previously you declare a = 0, you will never enter while , it would be advisable to use a do while , in addition to other improvements to the program

    
answered by 02.09.2018 в 02:41
0
  

Sorry for the delay, but I had an unforeseen event. Here you have something similar to what you wanted. If you have any questions, tell me in the comments and if you have solved the problem, you could indicate my answer as correct to help more people. A greeting.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

class producto {

    private String nombre;
    private double precio;

    public producto(String nombre, double precio) {
        this.nombre = nombre;
        this.precio = precio;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public double getPrecio() {
        return precio;
    }

    public void setPrecio(double precio) {
        this.precio = precio;
    }

    public void print() {

        System.out.println("---------------------------");
        System.out.println("Nombre: " + getNombre());
        System.out.println("Precio: " + getPrecio() +" euros");
    }

}

public class Producto_cesta {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        ArrayList<producto> cesta_productos = new ArrayList();

        boolean continuar = true;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String repetir = "s";

        while ("s".equals(repetir)) {

            try {

                System.out.println("Introduce el nomrbe del prodcuto");
                System.out.print("-> ");
                String nombre = br.readLine();

                System.out.println("Introduce el precio del prodcuto");
                System.out.print("-> ");
                double precio = Double.parseDouble(br.readLine());

                cesta_productos.add(new producto(nombre, precio));
            } catch (Exception e) {
                System.out.println("Error al introcudir los datos del producto = " + e);
            }

            repetir = "no se aun";
            while (!"s".equals(repetir) && !"n".equals(repetir)) {
                System.out.println("¿Quieres añadir un nuevo producto (s/n)?");
                System.out.print("->");
                try {
                    repetir = br.readLine();

                } catch (IOException e) {
                    System.out.println("Error Lectura = " + e);
                }
            }
        }

        System.out.println("#################################33");

        System.out.println("Los productos que has ingresado");
        double precio_total = 0;
        for (int i = 0; i < cesta_productos.size(); i++) {
            producto p = cesta_productos.get(i);
            p.print();
            precio_total += p.getPrecio();
        }
        System.out.println("===================================");
        System.out.println("PRECIO TOTAL = "+precio_total+" euros");
    }

}
    
answered by 02.09.2018 в 03:15
0

Indeed as I already indicated @Xam you have several errors regarding the logical operators, this section is fine:

import java.util.Scanner;

public class Punto3 {

public static void main(String[] args) {

    Scanner entrada=new Scanner (System.in);

After assigning an initial value of "0" for the variable "a", it will never enter your loop, so we assign a value of 1

double a=1;
double b=0;
double c=0;
double d=0;
double e=0;
double f=0;
double g=0;

Now yes, we can enter the loop

while (a==1) {

 System.out.println("hay mas productos?");
 System.out.println("1=si, 2 =no");
 a=entrada.nextDouble();

However, the second loop is also incorrect if you indicate that it evaluates that "a" is different from 1 or is different from 2, this will always be true, for example if a = 1 then it will be different from 2 and the loop will be cyclically infinitely, the same happens if "a" is 2, or any other value, I suggest you change it for this:

while (a<1 || a>2){
   System.out.println("no es un valor valido, vuelva a digitar");
   a=entrada.nextDouble();
}

Now since it is assured that "a" has a value of 1 or 2, we would break the cycle in case the value is 2, with this the "else break" of the final part of the code is no longer necessary

if (a==2) break;  

The rest of the code is correct

System.out.println("digite el valor del producto");
b=entrada.nextDouble();

System.out.println("cuantas unidades llevo de este producto?");
c=entrada.nextDouble();

    d=b*c;
    e=d+(d*16/100);

    if(e>=50000)
        e=e-(e*5/100);

    f=f+e;

   }

   System.out.println("el valor de su factura es:" +f);
 }

} 
    
answered by 02.09.2018 в 04:34