having a java code output error -
import java.util.scanner; public class strictdescending { public static void main( string[] args ) { final int sentinel = 1000; scanner n = new scanner(system.in); int prev = n.nextint(); if (prev >= sentinel) { system.out.println("empty list"); return; } while (true) { int next = n.nextint(); if (next >= sentinel) { break; } else if (next >= prev) { system.out.println("no, list not in descending order."); } prev = next; } system.out.println("yes, list in in descending order."); } }
i trying run program , go through list of 3 digit integers , when folowing 3 digit integer not descending want print no list not descending, if list in descending @ comes non 3 digit numbr 1000 want print yes list descending output works when put in non descending list of integers spits out 2 output statements
for input
150 140 140 120 1000
i
no, list not in descending order. no, list not in descending order. yes, list in in descending order.
when want
no, list not in descending order.
if i've understood problem description correctly, can solve setting flag, , use condition print either "yes, list in descending order." or "no, list not in descending order."
import java.util.scanner; public class strictdescending { public static void main(string[] args) { final int sentinel = 1000; scanner n = new scanner(system.in); int prev = n.nextint(); if (prev >= sentinel) { system.out.println("empty list"); return; } boolean descending = true; while (true) { int next = n.nextint(); if (next >= sentinel) { break; } else if (next >= prev) { descending = false; } prev = next; } if (descending) { system.out.println("yes, list in descending order."); } else { system.out.println("no, list not in descending order."); } } }
Comments
Post a Comment