Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is a code project that i'm working on to solve for difrent variables in the ideal gas law(PV=nRT). I am converting temperature to kelvin only if the tempeture is in celcus, and the user is not looking to solve for tempeture.

currently my code runs through the loop no matter what the input is even if it is "t".

public class Main {

    /**
     * @param args the command line arguments
     */
    //prints string to console
    private static void print(String s) {
        System.out.println(s);
    }

    //all the constants for given R values returns the correct R value
    private static double rValues(String pressure) {
        double R = 0;
        if (pressure.equalsIgnoreCase("atm")) {
            R = 0.082057338;
        } else if (pressure.equalsIgnoreCase("tor")) {
            R = 62.363577;
        } else if (pressure.equalsIgnoreCase("mmHg")) {
            R = 62.36367;
        }
        return R;
    }

    //all of the equations solved for unknown variable
    private static double solve_P(double[] variables, double constant, double temperature) {
        double exitP;
        exitP = (variables[2] * temperature * constant) / variables[1];
        return exitP;
    }

    private static double solve_V(double[] variables, double constant, double temperature) {
        double exitV;
        exitV = (variables[2] * temperature * constant) / variables[0];
        return exitV;
    }

    private static double solve_n(double[] variables, double constant, double temperature) {
        double exitN;
        exitN = (variables[0] * variables[1]) / (constant * temperature);
        return exitN;
    }

    private static double solve_T(double[] variables, double constant) {
        double exitT;
        exitT = (variables[0] * variables[1]) / (constant * variables[2]);
        return exitT;
    }

    public static void main(String[] args) {

        //play again loop
        boolean isRunning = true;
        String playAgain;
        while (isRunning) {

            Scanner keyboard = new Scanner(System.in);//creates scanner

            //introduction
            print("This is an ideal gas law solver");
            print("PV=nRT");

            //what R value to use
            print("what is the pressure measured in?");
            print("atm, tor, or mmmHg");
            print(">");
            String whatPressure;
            whatPressure = keyboard.next();
            double rConstant;
            rConstant = rValues(whatPressure);

            //stored questions
            String[] userQuestions = {"what is the pressure P",
                "what is the volume V",
                "what is the number of moles n"};

            //stores answers in userAnswers
            double[] userAnswers = new double[3];

            print("what variable would you like to solve for?");
            String unknownVariable;
            unknownVariable = keyboard.next();
            System.out.println("solving for " + unknownVariable);

            //converts between celsius and kelvin
            double temperature = 0;
            if (unknownVariable.compareToIgnoreCase("T") == 0) {
                print("what is the temperature in?");
                print("kelvin (K) or celsius (c) ");
                String whatTemperatureUnit;
                whatTemperatureUnit = keyboard.next();
                print("what is the temperature?");
                temperature = keyboard.nextDouble();
                if (whatTemperatureUnit.equalsIgnoreCase("c") || whatTemperatureUnit.equalsIgnoreCase("celsius")) {
                    temperature = temperature + 273.15;
                }
            }
        }
    }
}


What I have tried:

I am just learning and have made many changes however this is as far as I can get after trying for an hour. If there is anything that I can changes to make it run beter I would love to here it.
Posted
Updated 10-Apr-18 22:02pm
v2
Comments
wseng 9-Apr-18 22:09pm    
where is the loop ?
Richard MacCutchan 10-Apr-18 3:58am    
Your code runs start to end, but never provides any output, so how do you tell if it is working?
wseng 11-Apr-18 3:22am    
Would be better if you provide a desirable output and the current output you hava.

This is my full code

Java
<pre>package com.company;
import java.util.Scanner;

public class Main {
    //prints string to console
    private static void print(String s) {
        System.out.println(s);
    }

    //all the constants for given R values returns the correct R value
    private static double rValues(String pressure){
        double R = 0;
        if(pressure.equalsIgnoreCase("atm")){
            R = 0.082057338;
        }else if(pressure.equalsIgnoreCase("tor")){
            R = 62.363577;
        }else if(pressure.equalsIgnoreCase("mmHg")){
            R = 62.36367;
        }
        return R;
    }


    //all of the equations solved for unknown variable
    private static double solve_P(double[] variables, double constant, double temperature ){
        double exitP;
        exitP = (variables[2]*temperature*constant)/variables[1];
        return exitP;
    }


    private static double solve_V(double[] variables, double constant, double temperature){
        double exitV;
        exitV =(variables[2]*temperature*constant)/variables[0];
        return exitV;
    }


    private static double solve_n(double[] variables, double constant, double temperature){
        double exitN;
        exitN = (variables[0]*variables[1])/(constant*temperature);
        return exitN;
    }

    private static double solve_T(double[] variables, double constant){
        double exitT;
        exitT = (variables[0]*variables[1])/(constant*variables[2]);
        return exitT;
    }



    public static void main(String[] args) {

        //play again loop
        boolean isRunning = true;
        String playAgain;
        while (isRunning) {

            Scanner keyboard = new Scanner(System.in);//creates scanner

            //introduction
            print("This is an ideal gas law solver");
            print("PV=nRT");

            //what R value to use
            print("what is the pressure measured in?");
            print("atm, tor, or mmmHg");
            print(">");
            String whatPressure;
            whatPressure = keyboard.next();
            double rConstant;
            rConstant = rValues(whatPressure);

            //stored questions
            String[] userQuestions = {"what is the pressure P",
                    "what is the volume V",
                    "what is the number of moles n"};

            //stores answers in userAnswers
            double[] userAnswers = new double[3];

            print("what variable would you like to solve for?");
            String unknownVariable;
            unknownVariable = keyboard.next();
            System.out.println("solving for " + unknownVariable);

            //converts between celsius and kelvin
            double temperature = 0;
            if (unknownVariable.compareToIgnoreCase("T") == 0) {
                print("what is the temperature in?");
                print("kelvin (K) or celsius (c) ");
                String whatTemperatureUnit;
                whatTemperatureUnit = keyboard.next();
                print("what is the temperature?");
                temperature = keyboard.nextDouble();
                if (whatTemperatureUnit
 
Share this answer
 
Comments
Richard MacCutchan 11-Apr-18 4:32am    
Please do not post updates as Solutions. Delete this and use the Improve question link above to update your original post. And please add some useful detail about what happens; do not just say "it does not work".
Quote:
currently my code runs through the loop no matter what the input is even if it is "t".
It is not by mistake: your code is written to do that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900