I'm in my first programming class; can anyone help me understand why I can't print my last line please?
package program4;
import java.util.*;
public class Program4 {
public static void main(String[] args) {
int a, b, c, numComparisons;
String comparisons = "Comparisons for triangleType determination: ";
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 7; i++) {
}
String triangleType = "";
System.out.print("Enter 3 positive integer lengths for the sides of a "
+ "triangle:");
a = scan.nextInt();
b = scan.nextInt();
c = scan.nextInt();
System.out.println("The input lengths are: a = " + a + ", b = " + b + ", and"
+ " c = " + c + "");
if ((a + b < c) || (b + c < a) || (a + c < b)) {
System.out.print("There is no triangle with sides " + a + ", " + b + " and "
+ "" + c + ".");
} else {
numComparisons = 1;
comparisons += "a==b";
if (a == b) {
comparisons += "(T)" + "(b==c)";
numComparisons++;
if (b == c) {
comparisons += "(T)";
triangleType = "Equilateral";
}
} else {
comparisons += "(F)";
if (a == c) {
comparisons += "(T)";
triangleType = "Isosceles";
} else {
comparisons += "b==c";
numComparisons++;
comparisons += "(F)";
if (b == c) {
triangleType = "Isosceles";
} else {
comparisons += "a==c";
numComparisons++;
comparisons += "(F)";
triangleType = "Scalene";
}
}
}
System.out.printf("" + comparisons + (""));
System.out.printf("numComparisons = " + numComparisons);
System.out.println("The triangles with sides " + a + ", "
+ " + b + ", and " + c + ", is + triangleType + ");
}
}
}
Your last line syntax is pretty messed up.
this
System.out.println("The triangles with sides " + a + ", "
+ " + b + ", and " + c + ", is + triangleType + ");
should be
System.out.println("The triangles with sides " + a + ", "
+ b + ", and " + c + ", is " + triangleType);
System.out.println("The triangles with sides " + a + ", "
+ " + b + ", and " + c + ", is + triangleType + ");
What IDE/editor are you using? It should should show you the errors here. This should be
System.out.println("The triangles with sides " + a + ", "
+ b + ", and " + c + ", is" + triangleType);
This is better
Related
I am making a a mini game plugin, i need to replace every blocks that not a type, so i maked this :
public static void replaceBlock(String x, String y, String z, String x2, String y2, String z2, String block, String secondBlockType, String mapDict) {
String[] BlocksToReplace = mapDict.split("\\*");
System.out.println(ConsoleColorUtils.PURPLE + "[" + Main.pName +"]" + ConsoleColorUtils.RESET + " " + x + " " + y + " " + z + " " + x2 + " " + y2 + " " + z2);
ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
String pos1 = "/pos1 " + x + "," + y + "," + z;
String pos2 = "/pos2 " + x2 + "," + y2 + "," + z2;
Bukkit.dispatchCommand(console, pos1);
Bukkit.dispatchCommand(console, pos2);
tasak = Bukkit.getServer().getScheduler().runTaskTimer(Main.plugin, new Runnable() {
private int count = 2;
#Override
public void run() {
if(count == 0) {
for(String b : BlocksToReplace) {
if(b == block) {
System.out.println(ConsoleColorUtils.PURPLE + "[" + Main.pName +"]" + ConsoleColorUtils.RESET + " fdbshjfbdjshq ");
}else {
String fillcmd = "/replace " + b + " " + secondBlockType;
System.out.println(ConsoleColorUtils.PURPLE + "[" + Main.pName +"]" + ConsoleColorUtils.RESET + " " + fillcmd + " " + block);
Bukkit.dispatchCommand(console, fillcmd);
}
}
tasak.cancel();
} else {
count--;
}
}
}, 20, 20);
}
Map dict is => gold_block*wool:15*stained_hardened_clay:4*wool:4*stained_hardened_clay:3*stained_hardened_clay:11*wool:9*wool:11*stained_hardened_clay:9*wool:10*wool:2*stained_hardened_clay:10*stained_hardened_clay:2*stained_hardened_clay:6*diamond_block*prismarine:2*prismarine:1*melon_block*wool:5*slime*emerald_block*quartz_block*stained_hardened_clay*sandstone:2*nether_brick*wool:14*wool:13
block var is : stained_hardened_clay:14
When i call the function, it replace every blocks not all exept the block i want
Any way to fix it ?
Dont use "==" for strings. use b.equals(block). Because "==" compares the hash values of the String objects. And the equals Method compares the string itself.
More Info: https://www.java67.com/2012/11/difference-between-operator-and-equals-method-in.html
I have a segment of code that splits a string into tokens and prints them each out on a new line. I am having a hard time writing a code that determines if a word is a reserved word or not. I have to print "Reserved word is: " if the word is a java keyword, otherwise print "Current word is: ". Here is my code so far:
package projectweek3;
/**
*
* Name -
* Email Address -
* Date -
*
*/
public class Week3Project {
final static String program = "/*\n" +
" * To change this license header, choose License Headers in Project Properties.\n" +
" * To change this template file, choose Tools | Templates\n" +
" * and open the template in the editor.\n" +
" */\n" +
"package testapplication2;\n" +
"\n" +
"import java.util.Scanner;\n" +
"\n" +
"/**\n" +
" *\n" +
" * #author james\n" +
" */\n" +
"public class TestApplication2 {\n" +
"\n" +
" /**\n" +
" * #param args the command line arguments\n" +
" */\n" +
" public static void main(String[] args) {\n" +
" Scanner input = new Scanner(System.in);\n" +
" \n" +
" System.out.println(\"Enter integer #1\");\n" +
" int num1 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #2\");\n" +
" int num2 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #3\");\n" +
" int num3 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #4\");\n" +
" int num4 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #5\");\n" +
" int num5 = input.nextInt();\n" +
" \n" +
" //determine the sum\n" +
" int sum = num1 + num2 + num3 + num4 + num5;\n" +
" \n" +
" //this is helpful to make sure your sum is correct\n" +
" System.out.println(\"The sum is: \" + sum);\n" +
" \n" +
" //why doesn't this generate the sum correctly\n" +
" double average = sum / 5;\n" +
" \n" +
" //The average, lets hope its right...\n" +
" System.out.println(\"The average of your numbers is: \" + average);\n" +
" \n" +
" }\n" +
" \n" +
"}\n" +
"";
**public static void main(String[] args)
{
String str = program;
String s = "";
for (int i = 0; i < str.length(); i++) {
s += str.charAt(i) + "";
if (str.charAt(i) == ' ' || str.charAt(i) == '\t' || str.charAt(i) == '\n' || (str.charAt(i) == ' ' && str.charAt(i) == '\n')) {
String currentWord = s.toString();
String res = "int";
if (currentWord.equals(res)) {
System.out.println("Reserved word is: [" + currentWord + "]");
}
else {
System.out.println("Current word is: [" + currentWord + "]");
}
s = "";//Clear the string to get it ready to build next token.
}
}**
I would reconsider the way you're looping through the "program."
Instead of going through character by character, use the Java String.split() function.
String program = "int num1 = input.nextInt();\n";
String[] words = program.split("[\\n\\s\\t]");
for (String word : words) {
System.out.println(word);
}
Output:
int
num1
=
input.nextInt();
EDIT:
Since you can't use String.split(), your looping solution looks good. To check if the current word is reserved, try using Set.contains().
Set<String> reserved = new HashSet<>();
reserved.add("int");
// ...
if reserved.contains(word) {
System.out.println("Reserved word is: " + word);
} else {
System.out.println("Current word is: " + word);
}
That is, assuming you're allowed to use Set.
I'm not exactly a beginner in Java, but I'm also not an expert. Is there any way to shorten this code so that it takes up less space and possibly fewer lines?
JOptionPane.showMessageDialog(null, "The student's names are: "
+ roster[0][0] + " " + roster[1][0] + ", "
+ roster[0][1] + " " + roster[1][1] + ", "
+ roster[0][2] + " " + roster[1][2] + ", and "
+ roster[0][3] + " " + roster[1][3] + ".");
Let's refactoring the code step by step
introduce variables to remove duplication.
T[] col1 = roster[0];
T[] col2 = roster[1];
String content = col1[0] + " " + col2[0] + ", "
+ col1[1] + " " + col2[1] + ", "
+ col1[2] + " " + col2[2] + ", and "
+ col1[3] + " " + col2[3] + ".";
JOptionPane.showMessageDialog(null,"The student's names are: " + content);
separate string concatenation into multi-assignments, then all assignments are the same except the last one.
int i = 0;
String content = "";
content += col1[i] + " " + col2[i] + ", ";i++;
content += col1[i] + " " + col2[i] + ", ";i++;
content += col1[i] + " " + col2[i] + ", ";i++;
content += "and " + col1[i] + " " + col2[i] + ".";i++;
make the multi-assignments consistent by using ternary operator.
int i = 0;
String content = "";
content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", "); i++;
content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", "); i++;
content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", "); i++;
content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", "); i++;
using while-loop to remove duplication.
int i = 0;
String content = "";
while(i<=3) {
content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", ");
i++;
}
replacing while-loop with for-loop.
String content = "";
for (int i = 0; i <= 3; i++) {
content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", ");
}
introduce variables to make the code more readable.
String content = "";
for (int i = 0; i <= 3; i++) {
String prefix = i == 3 ? "and " : "";
String current = col1[i] + " " + col2[i];
String suffix = i == 3 ? "." : ", ";
content += prefix + current + suffix;
}
inline the variables col1 & col2 that is used only once:
String content = "";
for (int i = 0; i <= 3; i++) {
String prefix = i == 3 ? "and " : "";
String current = roster[0][i] + " " + roster[1][i];
String suffix = i == 3 ? "." : ", ";
content += prefix + current + suffix;
}
replace magic number 3 with constant and the final code is below:
final int last = 3;
String content = "";
for (int i = 0; i <= last; i++) {
String prefix = i == last ? "and " : "";
String suffix = i == last ? "." : ", ";
String current = roster[0][i] + " " + roster[1][i];
content += prefix + current + suffix;
}
JOptionPane.showMessageDialog(null, "The student's names are: " + content);
StringBuilder message = new StringBuilder("The student's names are: ");
for (int i = 0; i < roster[0].length; i++) {
message
.append(roster[0][i])
.append(" ")
.append(roster[1][i]);
if (i < roster[0].length - 1)
message.append(", ");
if (i == roster[0].length - 2)
message.append("and ")
}
message.append(".");
JOptionPane.showMessageDialog(null, message.toString());
Possibly something like that. As you can see, you don't really save any lines, but obviously the code is more flexible as it can account for a variable length roster.
You can do the following: Initially assign the normal statement that you do not want repeated. Then loop through roster.
String rosterString= "The student's names are: ";
for(int i=0;i<= roster.length;i++){
for(int j=0;j<= roster[i].length;j++){
rosterString += (roster[i][j] + " ");
if (i == 1 && j < 2) {
rosterString += ", ";
}
else if (i == 1 && j == 2) {
rosterString += ", and";
}
else if (i == 1 && j == 3) {
rosterString += ".";
}
else {
rosterString += " ";
}
}
}
And then pass rosterString to your method.
String rosterString = "";
for(int i = 0; i < roster[0].length; i++) {
rosterString += roster[0][i] + " " + roster[1][i] + ", ";
}
That code will create a string with the names and commas. You could then add in if statements to check if it is near the end to change the , to an and or .
I creating a program where it takes a base unit in the metric system. (Say grams.) And then when you select a prefix changes it to the equivalent amount. (Such as 1000 grams when you select Kilo would change it to 1 Kilogram.)
Problem is when I run the code it'll always end up as zero, which makes me think I'm manipulating the BigIntegers wrong. (I'm using VERY large numbers due to some prefixes being very small or very large beyond the usual long number.)
Here is the code:
import java.util.Scanner;
import java.math.BigInteger;
public class BaseMetricUnits {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello, World!");
float meter = 1;
float kilogram = 1;
float second = 1;
float ampere = 1;
float kelvin = 1;
float mole = 1;
float candela = 1;
PrefixConverter();
}
public static void PrefixConverter()
{
BigInteger Yocto = BigInteger.valueOf((long) 0.00000000000000000000000);
BigInteger Zepto = BigInteger.valueOf((long) 0.000000000000000000001);
BigInteger Atto = BigInteger.valueOf((long) 0.000000000000000001);
BigInteger Femto = BigInteger.valueOf((long) 0.000000000000001);
BigInteger Pico = BigInteger.valueOf((long)0.000000000001);
BigInteger Nano = BigInteger.valueOf((long)0.000000001);
BigInteger Micro = BigInteger.valueOf((long)0.000001);
BigInteger Milli = BigInteger.valueOf((long)0.001);
BigInteger Centi = BigInteger.valueOf((long)0.01);
BigInteger Deci = BigInteger.valueOf((long)0.1);
BigInteger Deca = BigInteger.valueOf((long)10);
BigInteger Hecto = BigInteger.valueOf((long)100);
BigInteger Kilo = BigInteger.valueOf((long)1000);
BigInteger Mega = BigInteger.valueOf((long)1000000);
BigInteger Giga = BigInteger.valueOf((long)1000000000);
BigInteger Tera = new BigInteger("1000000000000");
BigInteger Peta = new BigInteger("1000000000000000");
BigInteger Exa = new BigInteger("1000000000000000000");
BigInteger Zetta = new BigInteger("1000000000000000000000");
BigInteger Yotta = new BigInteger("1000000000000000000000000");
long Amount;
double Prefix;
String Units = "";
BigInteger translatedResult;
BigInteger Result;
Scanner inputDevice = new Scanner(System.in);
System.out.print("Please enter the type of unit to be used. (meters, grams, etc.) >> ");
Units = inputDevice.next();
System.out.print("Please enter an amount to be translated >> ");
Amount = inputDevice.nextLong();
System.out.print("Please choose one of the following Prefixes to translate to. ");
System.out.print(" 1 - Yocto ");
System.out.print(" 2 - Zepto ");
System.out.print(" 3 - Atto ");
System.out.print(" 4 - Femto ");
System.out.print(" 5 - Pico ");
System.out.print(" 6 - Nano ");
System.out.print(" 7 - Micro ");
System.out.print(" 8 - Milli ");
System.out.print(" 9 - Centi ");
System.out.print(" 10 - Deci ");
System.out.print(" 11 - Deca ");
System.out.print(" 12 - Hecto ");
System.out.print(" 13 - Kilo ");
System.out.print(" 14 - Mega ");
System.out.print(" 15 - Giga ");
System.out.print(" 16 - Tera ");
System.out.print(" 17 - Peta ");
System.out.print(" 18 - Exa ");
System.out.print(" 19 - Zetta ");
System.out.print(" 20 - Yotta ") ;
Prefix = inputDevice.nextDouble();
if(Prefix == 1)
{
Result = Yocto.multiply(BigInteger.valueOf(Amount));
translatedResult = Yocto.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Yocto" + Units + ".");
}
if(Prefix == 2)
{
Result = Zepto.multiply(BigInteger.valueOf(Amount));
translatedResult = Zepto.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Zepto" + Units + ".");
}
if(Prefix == 3)
{
Result = Atto.multiply(BigInteger.valueOf(Amount));
translatedResult = Atto.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Atto" + Units + ".");
}
if(Prefix == 4)
{
Result = Femto.multiply(BigInteger.valueOf(Amount));
translatedResult = Femto.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Femto" + Units + ".");
}
if(Prefix == 5)
{
Result = Pico.multiply(BigInteger.valueOf(Amount));
translatedResult = Pico.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Pico" + Units + ".");
}
if(Prefix == 6)
{
Result = Nano.multiply(BigInteger.valueOf(Amount));
translatedResult = Nano.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Nano" + Units + ".");
}
if(Prefix == 7)
{
Result = Micro.multiply(BigInteger.valueOf(Amount));
translatedResult = Micro.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Micro" + Units + ".");
}
if(Prefix == 8)
{
Result = Milli.multiply(BigInteger.valueOf(Amount));
translatedResult = Milli.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Milli" + Units + ".");
}
if(Prefix == 9)
{
Result = Centi.multiply(BigInteger.valueOf(Amount));
translatedResult = Centi.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Centi" + Units + ".");
}
if(Prefix == 10)
{
Result = Deci.multiply(BigInteger.valueOf(Amount));
translatedResult = Deci.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Deci" + Units + ".");
}
if(Prefix == 11)
{
Result = Deca.multiply(BigInteger.valueOf(Amount));
translatedResult = Deca.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Deca" + Units + ".");
}
if(Prefix == 12)
{
Result = Hecto.multiply(BigInteger.valueOf(Amount));
translatedResult = Hecto.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Hecto" + Units + ".");
}
if(Prefix == 13)
{
Result = Kilo.multiply(BigInteger.valueOf(Amount));
translatedResult = Kilo.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Kilo" + Units + ".");
}
if(Prefix == 14)
{
Result = Mega.multiply(BigInteger.valueOf(Amount));
translatedResult = Mega.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Mega" + Units + ".");
}
if(Prefix == 15)
{
Result = Giga.multiply(BigInteger.valueOf(Amount));
translatedResult = Giga.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Giga" + Units + ".");
}
if(Prefix == 16)
{
Result = Tera.multiply(BigInteger.valueOf(Amount));
translatedResult = Tera.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Tera" + Units + ".");
}
if(Prefix == 17)
{
Result = Peta.multiply(BigInteger.valueOf(Amount));
translatedResult = Peta.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Peta" + Units + ".");
}
if(Prefix == 18)
{
Result = Exa.multiply(BigInteger.valueOf(Amount));
translatedResult = Exa.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Exa" + Units + ".");
}
if(Prefix == 19)
{
Result = Zetta.multiply(BigInteger.valueOf(Amount));
translatedResult = Zetta.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Zetta" + Units + ".");
}
if(Prefix == 20)
{
Result = Yotta.multiply(BigInteger.valueOf(Amount));
translatedResult = Yotta.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Yotta" + Units + ".");
}
else
{
System.out.println("Not a valid input.");
}
}
}
Thanks for your help.
BigInteger can only store integers, and so is not a suitable data type for this application. I strongly recommend replacing it with BigDecimal.
You should also use string representations of the fractions to initialize:
BigDecimal Zepto = new BigDecimal("0.000000000000000000001");
BigDecimal Atto = new BigDecimal("0.000000000000000001");
BigDecimal Femto = new BigDecimal("0.000000000000001");
BigDecimal Pico = new BigDecimal("0.000000000001");
BigDecimal Nano = new BigDecimal("0.000000001");
Comments on this answer and on the question indicate a concern with using BigDecimal to store the larger numbers. This program illustrates the fact that it is not an issue:
import java.math.BigDecimal;
public class Test {
public static void main(String[] args) {
BigDecimal googol = new BigDecimal("1e100");
System.out.println(googol);
System.out.println(googol.add(BigDecimal.ONE));
}
}
Output:
1E+100
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
how can I initialize all of my variables more easier?
is there a calculus package?
is there a more efficient solution overall?
import java.util.Scanner;
public class limits {
public static void main(String[] args)
{
String introMessage = " ***Calculus: Limits***" + "\n"
+ "This application uses the method of exhaustion" + "\n"
+ "to test limits. You enter the number that x" + "\n"
+ "approches and this program will give you three" + "\n"
+ "numbers on either side of the limit showing" + "\n"
+ "closer approximations of the limit.";
System.out.println(introMessage);
System.out.println();
String polynomialMessage = "Our function: " + "\n"
+ " lim f(x) = x^2 + x + 1 = L" + "\n"
+ "x -> a";
System.out.println(polynomialMessage);
System.out.println();
System.out.println("As x approaches a, what will our limit L be?");
System.out.println();
can I initialize all of these at once?
// initialize variables
double belowAOne = 0.0;
double belowATwo = 0.0;
double belowAThree = 0.0;
double belowAFour = 0.0;
double aboveAOne = 0.0;
double aboveATwo = 0.0;
double aboveAThree = 0.0;
double aboveAFour = 0.0;
double totalBAOne = 0.0;
double totalBATwo = 0.0;
double totalBAThree = 0.0;
double totalBAFour = 0.0;
double totalAAOne = 0.0;
double totalAATwo = 0.0;
double totalAAThree = 0.0;
double totalAAFour = 0.0;
double L = 0;
// create a Scanner object named sc
Scanner sc = new Scanner(System.in);
// perform invoice calculations until choice isn't equal to "y" or "Y"
String choice = "y";
while (!choice.equalsIgnoreCase("n"))
{
System.out.println("Please enter a whole number between 1 and 10 for a: ");
double a = sc.nextDouble();
if (a > 0 && a <=10 )
{
// calculate L
L = (a*a) + a + 1;
// create values that approaches a
belowAOne = a - .5;
belowATwo = a - .1;
belowAThree = a - .01;
belowAFour = a - .001;
aboveAOne = a + .5;
aboveATwo = a + .1;
aboveAThree = a + .01;
aboveAFour = a + .001;
totalBAOne = (belowAOne * belowAOne) + belowAOne + 1;
totalBATwo = (belowATwo * belowATwo) + belowATwo + 1;
totalBAThree = (belowAThree * belowAThree) + belowAThree + 1;
totalBAFour = (belowAFour * belowAFour) + belowAFour + 1;
totalAAOne = (aboveAOne * aboveAOne) + aboveAOne + 1;
totalAATwo = (aboveATwo * aboveATwo) + aboveATwo + 1;
totalAAThree = (aboveAThree * aboveAThree) + aboveAThree + 1;
totalAAFour = (aboveAFour * aboveAFour) + aboveAFour + 1;
String chart = " x " + "x^2 + x + 1" + "\n"
+ "---------+--------------" + "\n"
+ " " + belowAOne + " : " + totalBAOne + "\n"
+ " " + belowATwo + " : " + totalBATwo + "\n"
+ " " + belowAThree + " : " + totalBAThree + "\n"
+ " " + belowAFour + " : " + totalBAFour + "\n"
+ " " + " a " + " : " + "L" + "\n"
+ " " + aboveAFour + " : " + totalAAFour + "\n"
+ " " + aboveAThree + " : " + totalAAThree + "\n"
+ " " + aboveATwo + " : " + totalAATwo + "\n"
+ " " + aboveAOne + " : " + totalAAOne + "\n";
System.out.println();
System.out.println();
System.out.println();
System.out.println(chart);
System.out.println();
System.out.println("As X approaches " + a + ", our guess "
+ "for L is: " + L);
System.out.println();
// end the program
choice = "n";
}
else
{
System.out.println();
System.out.println("***Invalid Entry***");
System.out.println();
}
}
}
}
Is there a better way to write this program?
or you could use a loop instead like this:
double[] belowA = {a - .5, a - .1, a - .01, a - .001};
double[] totalBA = new double[4];
for(int i = 0; i < 4; ++i) {
totalBA[i] = (belowA[i] * belowA[i]) + belowA[i] + 1;
}
also with a StringBuilder / StringBuffer
and finally you could create a method to handle below/above in the same way without duplicated code