Wikipedia:Reference desk/Archives/Computing/2020 July 27

Computing desk
< July 26 << Jun | July | Aug >> July 28 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


July 27

edit

Java!

edit

Hi guys, can you help me figure out how could the first line of the output was correct and the others were not? Thank you! Deep humility (talk) 19:06, 27 July 2020 (UTC)[reply]

// Print a conversion table of inches to meters.
// Display 12 feet of conversions, inch by inch. 
// One meter equals approximately 39.37 inches. 

/* 1 inch = feet =  meters;
 * 2 inches = feet = meters;
 * 3 inches = feet = meters;  
 * n inches = n feet = n meters;
 * 
 * 144 inches = 12 feet = meters;
 * */

/* 1 meter = 39.37 inches. 
 * 
 * (1/39.37) meter = 1 inch. 
 * */

/* 1 feet = 12 inches; 
 * 
 * (1/12) feet = 1 inch;
 * */

/* 1 inch = (1/12) feet = (1/39.37) meters;
 * 2 inches = 2 * (1/12) feet = 2 * (1/39.37) meters;
 * 3 inches = 3 * (1/12) feet = 3 * (1/39.37) meters;  
 * n inches = n * (1/12) feet = n * (1/39.37) meters;
 * 
 * 144 inches = 12 feet = 3.6576 meters;
 * */


	public static void main(String[] args) {

		// TODO Auto-generated method stub

		int counter = 1;
		int n = 1; // multiple;

		for (int inch = 1; inch < 145; inch++) {

			float feet = (float) ((n) * (inch / 12.00));
			float meter = (float) ((n) * (inch / 39.37));

			if (inch == 1) {

				System.out.println(inch + " inch is " + feet + " feet or" + meter + " meters.");

			} else {

				System.out.println(inch + " inches equal " + feet + " feet or " + meter + " meters.");

			}

			n = n + 1;


		}

	}

Output:

1 inch is 0.083333336 feet or 0.02540005 meters.  

2 inches equal 0.33333334 feet or 0.1016002 meters.  


3 inches equal 0.75 feet or 0.22860046 meters.  


4 inches equal 1.3333334 feet or 0.4064008 meters.  


5 inches equal 2.0833333 feet or 0.63500124 meters.  

What is "n"? Why are you incrementing both inch and n? -- Finlay McWalter··–·Talk 19:17, 27 July 2020 (UTC)[reply]

Solved:

What a careless mistake! I ignored that inch is incremental due to inch++. 😳😳😵😵😬😬😱😱 Deep humility (talk) 19:19, 27 July 2020 (UTC)[reply]

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int counter = 1;
		int n = 1; // multiple;

		for (int inch = 1; inch < 145; inch++) {

			float feet =  (float) (inch / 12.00);
			float meter =  (float) (inch / 39.37);

			if (inch == 1) {

				System.out.println(inch + " inch is " + feet + " feet or " + meter + " meters.");

			} else {

				System.out.println(inch + " inches equal " + feet + " feet or " + meter + " meters.");

			}

			n = n + 1;

			

		}

	}

User:Finlay McWalter, I modified my code a bit and the output appeared to be correct now. Thank you for your attention and enlightenment! Deep humility (talk) 19:21, 27 July 2020 (UTC)[reply]

Nitpicking: one meter is not 39.37 inches. Instead, one inch is defined to be exactly 0.0254 meters and one foot is exactly 0.3048 meters. 93.142.89.209 (talk) 22:02, 27 July 2020 (UTC)[reply]
To be fair, 1 m was exactly 39.37 inches in the US... until 1959 when the inch was redefined. See Foot (unit)#Definition. --174.89.49.204 (talk) 22:42, 27 July 2020 (UTC)[reply]
Indeed it was. One learns something new every day! 93.142.89.209 (talk) 04:25, 28 July 2020 (UTC)[reply]
Oh! Thanks for the information. I was just using my reference e-book's tip! Deep humility (talk) 15:42, 28 July 2020 (UTC)[reply]
Your code is correct now, but note also that you don't need the variables counter and n for anything. JIP | Talk 10:36, 28 July 2020 (UTC)[reply]
Ahh, yes, you are right. Thank you! Deep humility (talk) 15:42, 28 July 2020 (UTC)[reply]