java


How would I correct the program so it runs correctly?


This program should have a “while” loop that asks repeatedly for a number until a number divisible by 6 or 17 is given. When that occurs, the program should display the total number of numbers read that are not divisible by 6 or 17.
import java.util.Scanner;
public class DivisibleBy6or17 {
public static void main(String[] args) {
Scanner kbd = new Scanner (System.in);
System.out.print("Enter Value : ");
int nbr = kbd.nextInt();
int count = 0;
while ((nbr % 7 ==0 ) && (nbr % 16 == 0)){
count++;
System.out.print("Enter Value: ");
nbr = kbd.nextInt();
}
System.out.println("Numbers read : " + count);
}
}
Try with
while (!((nbr % 7 ==0 ) && (nbr % 16 == 0))){
You want to do while it is False
Condition is wrong, it should be an or operand on the while loop.
import java.util.Scanner;
public class DivisibleBy6or17 {
public static void main(String[] args) {
Scanner kbd = new Scanner (System.in);
System.out.print("Enter Value : ");
int nbr = kbd.nextInt();
int count = 0;
while (!((nbr % 17 == 0 ) || (nbr % 6 == 0))){
count++;
System.out.print("Enter Value: ");
nbr = kbd.nextInt();
}
System.out.println("Numbers read : " + count);
}
}
Try using or (||) instead of and (&&)
Change
while ((nbr % 7 ==0 ) && (nbr % 16 == 0)){
count++;
System.out.print("Enter Value: ");
nbr = kbd.nextInt();
}
To
/* while nbr **isn't** divisible by 7 or 16 */
while (! ((nbr % 7 == 0 ) || (nbr % 16 == 0)) ){
count++;
System.out.print("Enter Value: ");
nbr = kbd.nextInt();
}
Try this
Not sure whether the numbers are right, you question mentions numbers different to the code. I've used the ones in the code.
import java.util.Scanner;
public class DivisibleBy6or17 {
public static void main(String[] args) {
Scanner kbd = new Scanner (System.in);
int count = 0;
//loop forever
while ( true ) {
//prompt
System.out.print("Enter Value : ");
int nbr = kbd.nextInt();
//if number is divisible
if ( nbr % 7 == 0 || nbr % 16 == 0 ) {
//leave loop
break;
} else {
//add to count
count++;
}
}
System.out.println("Numbers read : " + count);
}
}

Related Links

Java application Problem - Net beans
parsing large XML using SAX in java
Efficient BigDecimal round Up and down to two decimals
Using AOP to track code usage patterns in a Java Application
Non-Confusing Simple Validation of email in Java String
OSGi : Is there a 'best way' to handle service and registers?
How to run windows console application in java code using exec() method?
Renaming the JTree Node Manually in Java
405 http method get is not supported by this url
Expanding Java SWT Tree asynchronously
couldn't get lock for .log file in Java
How to remove duplicate objects in a List<MyObject> without equals/hashcode?
What is a good cross platform Java web server for development?
How to have the user choose an audio file and play it in Java
My PrintWriter's value becomes void when used in different classes
how to rerender a dynamic id (nested within datalist) in jsf/a4j?

Categories

HOME
compiler-construction
asp.net-core
netbeans
omnet++
minimum-spanning-tree
c#-4.0
react-virtualized
platform-builder
jsrender
survey
echarts
tomcat6
spring-kafka
wheelnav.js
dax
visual-studio-cordova
worldwind
contextmenu
wkwebview
lcd
dynamic-featured-image
user-interaction
linkerd
junit5
google-qpx-express-api
http-get
gammu
mapbox-gl
nssegmentedcontrol
automake
occlusion
trim
email-templates
core-plot
thin
youcompleteme
alphabet
typescript1.8
idisposable
jxcore
snmptrapd
dtexec
sqlbulkcopy
sqldf
color-profile
blacklist
or-tools
medium.com
deadbolt-2
color-picker
autorest
player
contact-list
simplewebrtc
sigabrt
qgraphicsview
dataview
qtableview
msys
coveralls
embedded-code
responsive-images
ideamart
interrupted-exception
generic-programming
sortedlist
device-orientation
apache-commons-net
nsmutabledictionary
offloading
c3
nstableviewcell
android-imagebutton
relocation
qcodo
onactivityresult
ceil
industrial
access-rights
runtime.exec
web2py-modules
shellexecute
sabredav
smtp-auth
google-email-migration
dbproviderfactories
parametric-equations
enter
custom-backend
invite
yetanotherforum
lpeg
getresponsestream
avatar
thread-local-storage
mediarss
grid-system

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App