java


Basic Looping in Java


i'am junior programmer student. so confuse with making loop program with output below with align right :
54321
4321
321
21
1
but i've try this one :
for (int i=1; i<=5;i++){
for (int j=1; j<=i;j++) {
System.out.print(" ");
}
for (int k=i; k<=5;k++) {
System.out.print(k);
}
System.out.println();
}
But the output below (align right works):
12345
2345
345
45
5
How, I've 98 try edits the code, but keep fail. What should I do?
Here is a simpler version, using decrementing loops and the ternary conditional operator to choose whether to print number or space.
for (int i = 5; i > 0; i--) {
for (int j = 5; j > 0; j--)
System.out.print(j > i ? " " : j);
System.out.println();
}
change your logic to this:
for(int i=5;i>0;i--){
for(int k=i;k>0;k--){
System.out.print(k);
}
System.out.println("");
}

Related Links

Java - Pattern matches but fails to capture
Storing fibonacci in an array and printing the user desired value
Copy the contents of a JPanel onto a BufferedImage
(Java) Can anyone explain to me why my code does not draw an oval in my GUI
Spring java ResponseEntity<T> with multiple parameter types
findViewByIdlayoutinflator fails to inflate my layouts, returns null when using
Unable to reference BCrypt File from JSP
How would I make my random number appear instantly on my frame?
every time i replace fragment with another number of fragment in my fragmentManager increases
Java GUI Advice
Is it possible to make an ArrayList that only accepts objects that implement a specific interface? [closed]
Java Slick2D Line Constructor throws NullPointerException
How to use java 8 merge function for n number of hashmaps
Reading multiple files in a folder using JAVA
HTTP Status 404, The requested resource () is not available spring maven project
StringTokenizer “NoSuchelementException”

Categories

HOME
cakephp
openstack
azure-data-factory
include
grep
mean-stack
gis
frameworks
win32gui
retrofit
indesign
dacpac
u-sql
size
hex-editors
msp430
iolanguage
graphlab
contextmenu
scichart
restful-authentication
pepper
status
facebook-access-token
nhapi
webkitspeechrecognition
subdomains
librato
framemaker
google-api-nodejs-client
amazon-kinesis-kpl
simplexml
abstract-class
hilbert-curve
leading-zero
email-templates
reportingservices-2005
glew
nand2tetris
spring-security-kerberos
btrace
flickr-api
multipeer-connectivity
togetherjs
aurelia-validation
np-complete
account-kit
messenger
measures
paypal-express
ios4
endeca-workbench
tarjans-algorithm
device-orientation
nsmutabledictionary
operator-precedence
wordpress-theme-customize
google-reader
hippomocks
ivyde
aqtime
flash-builder4.5
limejs
jelly
sublist
libstdc++
dsn
twrequest
asyncfileupload
adk
php-parser
xdomainrequest
horizontal-accordion
disclosure
oncheckedchanged
mysql-error-1005
ntvdm.exe

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile