------K-Nearest-Neighbor Prediction Program---
The values you entered are : sunny cool high true no
Comparring the values : sunny hot high FALSE dist = 0 and play = no
Comparring the values : sunny hot high TRUE dist = 0 and play = no
Comparring the values : overcast hot high FALSE dist = 0 and play = yes
Comparring the values : rainy mild high FALSE dist = 0 and play = yes
Comparring the values : rainy cool normal FALSE dist = 0 and play = yes
Comparring the values : rainy cool normal TRUE dist = 0 and play = no
Comparring the values : overcast cool normal TRUE dist = 0 and play = yes
Comparring the values : sunny mild high FALSE dist = 0 and play = no
Comparring the values : sunny cool normal FALSE dist = 0 and play = yes
Comparring the values : rainy mild normal FALSE dist = 0 and play = yes
Comparring the values : sunny mild normal TRUE dist = 0 and play = yes
Comparring the values : overcast mild high TRUE dist = 0 and play = yes
Comparring the values : overcast hot normal FALSE dist = 0 and play = yes
Comparring the values : rainy mild high TRUE dist = 0 and play = no
Given the data, the prediction to play is: no
我的问题是距离没有增加。 它假设按以下顺序降序:2,1,3,3,3,2,2,2,2,4,2,2,4,2但我全部为零。
这是我的代码..... :::
import java.util.Scanner;
import java.io.*;
public class lab2
{
public static void main (String [] args)
throws FileNotFoundException
{
Scanner input = new Scanner (System.in);
Scanner in = new Scanner(new File("data.txt"));
String queryOutlook = " sunny ";
String queryHumidity = " high ";
String queryTempurature = " hot ";
String queryWind = " true ";
String queryPlay = " play ";
String outlook = null;
String humidity = null;
String tempurature = null;
String wind = null;
String play = null;
int dist = 0;
System.out.print("------K-Nearest-Neighbor Prediction Program---\n\n");
System.out.print("The values you entered are : ");
outlook = input.next();
tempurature = input.next();
humidity = input.next();
wind = input.next();
play = input.next();
while(in.hasNext())
{
outlook = in.next();
tempurature = in.next();
humidity = in.next();
wind = in.next();
play = in.next();
dist = 0;
if (outlook.equalsIgnoreCase(queryOutlook) )
dist++;
if (humidity.equalsIgnoreCase(queryHumidity) )
dist++;
if (tempurature.equalsIgnoreCase(queryTempurature) )
dist++;
if (wind.equalsIgnoreCase(queryWind) )
dist++;
if (play.equalsIgnoreCase(queryPlay) )
dist++;
System.out.printf("\r\nComparring the values : %8s %8s %8s %8s", outlook, tempurature, humidity, wind);
System.out.printf(" dist = %1d and play = %3s ",dist,play);
}
System.out.printf("\nGiven the data, the prediction to play is: %3s \n",play);
in.close();
}
}
答案 0 :(得分:2)
默认情况下,当抓取令牌时,扫描程序会忽略空格,因此.equals失败,因为常量中有空格。