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

Include fxml file to executable jar
What should I learn to be able to transform expression in Java to another expression (like interpreter does)? [closed]
spring 4.3.7 and weblogic 12.2.1 Rest integration has error
FileOutputStream change `0A` byte to `0D 0A`
App crashes when calculating distance using maps API [duplicate]
Consul and Spring Boot services in Docker - not deregistering
Spring hibernate unwanted timezone adjustment
IllegalStateException: Implementation not found when trying to obtain a class
Needs help setting up Spring/Hibernate Mysql settings
Repainting an Ellipse, JAVA Swing/AWT
Sorting reviews properly in a Java List using Bayesian estimate
Store multiple ordinary differential equations outputs using StepHandlers with classical runge-kutta integrator
JSF 2.2: Not able to select input text box after closing the p:dialog modal dialog in Mozilla FireFox
Java: Sort string list by a number contained within that string (descending)
JavaFX, tableview's data are not dysplaying
Modality and Ownership JavaFX-8 Windows

Categories

HOME
variables
oracle11g
appx
dot
grep
q
cvs
react-redux
opengl-es-2.0
slick-slider
user-input
modelica
timeout
spring-tool-suite
ups
iron-router
google-cloud-speech
viewport
firefox-webextensions
xlsxwriter
chromium-embedded
realex-payments-api
javacv
fifo
subdomains
instant-messaging
r-raster
yadcf
xquery-3.0
google-sites-2016
fog
copying
pim
ios5
hot-module-replacement
abstract-class
starteam
ws-security
android-tabhost
core-plot
password-encryption
keydown
import-from-excel
segment
skeleton-css-boilerplate
taffy
sage-one
statsd
diagnostics
mako
bind9
minimization
smart-table
gnome-shell-extensions
yt-project
gridpane
azure-sdk
ado.net-entity-data-model
file-writing
skobbler-maps
django-debug-toolbar
angstrom-linux
netmq
endeca-workbench
angular-leaflet-directive
emailrelay
cakephp-3.1
php-ci
method-parameters
ami
dereference
inmobi
java-metro-framework
php-5.4
project-planning
dml
valueconverter
undefined-reference
app42
prettify
flexmojos
page-layout
geos
quantlib-swig
inbox
padarn
chronoforms
mqx
enterprisedb
jelly
selected
itmstransporter
hamiltonian-cycle
chuck
trusted
gdata-api
word-processor
heartbeat
infobox
propagation
sql-server-profiler
remember-me
sudzc
paster
sproutcore-2
revisions
dbal
3gp

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App